我在使用EsonCLI和Rails时使用Active Model Serializers 0.10.x,同时尝试将Json-Api作为适配器.GET请求正在努力,但反序列化的活动模型甚至没有,虽然我试图落实jimbeaudoin描述的轨道strong_parameters解决方案在这里.
我保存评论的最新尝试:
有效载荷:
{"data":{
"attributes": {"soft_delete":false,"soft_delete_date":null,"text":"hjfgfhjghjg","hidden":false,"empathy_level":0},
"relationships":{
"user":{"data":{"type":"users","id":"1"}},
"post":{"data":{"type":"posts","id":"1"}}},"type":"comments"}}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
Completed 400 Bad Request in 13ms (ActiveRecord: 8.6ms)
ActionController::ParameterMissing (param is missing or the value is empty: data):
Run Code Online (Sandbox Code Playgroud)
评论控制器:
class Api::V1::CommentsController < MasterApiController
respond_to :json
...
def create
render json: Comment.create(comment_params)
end
...
def comment_params
#Deserialization issues... Waiting for #950 https://github.com/rails-api/active_model_serializers/pull/950
params.require(:data).require(:attributes).permit(:text, :user_id, :post_id, :empathy_level, :soft_delete_date, :soft_delete, :hidden)
end
end
Run Code Online (Sandbox Code Playgroud)
注意到如果我将参数设置为只有params.permit(...),服务器会将所有内容保存为null(我现在没有在注释模型上设置任何约束):
data: {id: "9", type: "comments",…}
attributes: {soft_delete: null, soft_delete_date: null, text: null, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Active Model Serializer构建一些Rails模型的JSON表示,其中一些模型嵌入了其他模型.例如,我有Event和Attendees,Event has_and_belongs_to_many Attendees.
class EventSerializer < ActiveModel::Serializer
attributes :name
has_many :attendees, serializer: AttendeeSerializer
end
class AttendeeSerializer < ActiveModel::Serializer
attributes :name
end
Run Code Online (Sandbox Code Playgroud)
这会导致JSON之类的{ name: 'Event One', attendees: [{ name: 'Alice' }, { name: 'Bob' }] }.
现在,我想补充与会者对此次活动所说的内容.让我们说,评论belongs_to事件,belongs_to参加者.我想在事件的序列化输出中包含所述注释,因此它会成为{ name: 'Event One', attendees: [{ name: 'Alice', comments: [{ text: 'Event One was great!'}] }, { name: 'Bob', comments: [] }] }.
我本可以有
class AttendeeSerializer < ActiveModel::Serializer
attributes :name
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
但这会选择此与会者对所有活动的所有评论 - 而不是我想要的.我想写这个,但是如何找到我正在进行序列化的特定事件?我可以以某种方式访问'父'对象,或者可能将选项传递给has_many序列化程序?
class AttendeeSerializer < …Run Code Online (Sandbox Code Playgroud) 我正在使用ActiveModel :: Serializers构建API .使用params有条件地侧载数据的最佳方法是什么?
所以我可以提出如下请求GET /api/customers:
"customers": {
"first_name": "Bill",
"last_name": "Gates"
}
Run Code Online (Sandbox Code Playgroud)
和 GET /api/customers?embed=address,note
"customers": {
"first_name": "Bill",
"last_name": "Gates"
},
"address: {
"street": "abc"
},
"note": {
"body": "Banned"
}
Run Code Online (Sandbox Code Playgroud)
这样的东西取决于params.我知道ActiveModel :: Serializers有include_[ASSOCIATION]?语法,但我如何从我的控制器有效地使用它?
这是我目前的解决方案,但它并不整洁:
customer_serializer.rb:
def include_address?
!options[:embed].nil? && options[:embed].include?(:address)
end
Run Code Online (Sandbox Code Playgroud)
application_controller.rb:
def embed_resources(resources = [])
params[:embed].split(',').map { |x| resources << x.to_sym } if params[:embed]
resources
end
Run Code Online (Sandbox Code Playgroud)
customers_controller.rb:
def show
respond_with @customer, embed: embed_resources
end
Run Code Online (Sandbox Code Playgroud)
必须是一个更简单的方法?
我正在使用active_model_serializer.现在我想用分页序列化一个对象,我应该在控制器或串行器中进行分页逻辑吗?
如果我选择在序列化程序中进行分页,我需要将page_number和per_page传递给序列化程序.我该怎么办?我的理解是序列化程序只接受模型对象.
serialization pagination ruby-on-rails active-model-serializers
如果我想从默认属性适配器切换到json api适配器,我会在哪里这样做?
入门指出:
一般来说,作为AMS的用户,您将编写(或生成)这些序列化程序类.如果要使用其他适配器(例如JsonApi),可以在初始化程序中更改它:
ActiveModel::Serializer.config.adapter = :json_api
Run Code Online (Sandbox Code Playgroud)
他们指的是什么初始化器?我要创建一个新的吗?抱歉,这个菜鸟问题
ruby json ruby-on-rails active-model-serializers ruby-on-rails-4
我遇到了一个特殊情况,即ActiveModel :: Serializer生成的渲染json非常慢(大约6-8秒).如何提高渲染速度?这是代码.
楷模:
class Comment < ActiveRecord::Base
has_many :children_comments,
class_name: 'Comment',
foreign_key: 'parent_comment_id'
belongs_to :user
belongs_to :parent_comment,
class_name: 'Comment',
foreign_key: 'parent_comment_id'
end
Run Code Online (Sandbox Code Playgroud)
串行器:
class CommentSerializer < ActiveModel::Serializer
include ActionView::Helpers::DateHelper
attributes :id, :message, :created_at_in_words,
:created_at, :parent_comment_id
belongs_to :user
has_many :children_comments
def created_at_in_words
time_ago_in_words(object.created_at) + ' ago'
end
def children_comments
object.children_comments.map do |comment|
CommentSerializer.new(comment).as_json
end
end
end
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :avatar_url
def avatar_url
object.avatar.url
end
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我有
parent_comments = Comment.where(parent_comment_id: nil)
render status: :ok,
json: parent_comments,
each_serializer: …Run Code Online (Sandbox Code Playgroud) serialization json ruby-on-rails active-model-serializers ruby-on-rails-4
我有一个带有深层嵌套关联的rails应用程序.
.-< WorkPeriod
Timecard -< Week -< Day -<--< Subtotal
`-< Adjustment
-< (has many)
Run Code Online (Sandbox Code Playgroud)
我正在使用Active Model Serializer来构建API.
在客户端,我想一次性加载时间卡和所有关联.
目前我的序列化器看起来像这样,
class TimecardSerializer < ActiveModel::Serializer
embed :ids, include: true
has_many :weeks
end
class WeekSerializer < ActiveModel::Serializer
embed :ids, include: true
has_many :days
end
# ... etc ...
Run Code Online (Sandbox Code Playgroud)
这一切都找到了,除了没有什么急于加载.因此,它最终会针对每个请求对数据库进行大量调用.对于每周,它会单独请求该周的日期.并且对于每一天,它单独请求它的work_periods,小计和调整.
performance ruby-on-rails associations eager-loading active-model-serializers
我试图在Active_model_serializer中为Ember应用程序加载数据,并在尝试包含对象时获取NoMethodError:
#Email的未定义方法`object':0x00000100d33d20
它只发生在:include => true设置,如下所示:
class ContactSerializer < ActiveModel::Serializer
embed :ids, :include => true
attributes :first_name, :last_name
has_many :emails
end
Run Code Online (Sandbox Code Playgroud)
我的模型看起来像这样:
class Contact < ActiveRecord::Base
attr_accessible :first_name, :last_name, :company,
belongs_to :account
belongs_to :user
has_many :emails
end
class Email < ActiveRecord::Base
attr_accessible :email_address, :email_type_id, :is_primary
belongs_to :contact
end
Run Code Online (Sandbox Code Playgroud)
我的控制器看起来像这样:
def show
@contact = @current_user.contacts.where(:id => params[:id]).includes(:emails).first
render :json => @contact
end
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我正在尝试发送我的前端应用程序json,如下所示:
{
facilities: [
{id: 5, name: 'happy days ranch', location: { address: '1424 Pastoral Lane', zipcode: '25245'}, instructor_ids: [2, 4, 9]}
],
instructors: [
{id: 4, name: 'Johnny Pheonix', skill: '8', picture: 'aws_url', facility_ids: [5, 8, 12}
]
}
Run Code Online (Sandbox Code Playgroud)
render :json => @facilities
Run Code Online (Sandbox Code Playgroud)
串行器发现了这一点.好极了!但这并不包括任何教师
render :json => {facilities: @facilities, instructors: @instructors}
Run Code Online (Sandbox Code Playgroud)
这给了我一个教师数组和一个工具数组,但是没有使用activeModel :: Serializers.
render :json => [@facilities, @instructors]
Run Code Online (Sandbox Code Playgroud)
起初我很兴奋这个,因为它给了我两个数组,它使用了ActiveModel :: Serializers.但是,这就是JSON的样子:
{facilities: [
{facilities: [
#my facilities data
]},
{facilities: [
#my instructor data
]}
]}
Run Code Online (Sandbox Code Playgroud)
json activemodel ruby-on-rails-3 ember.js active-model-serializers
说我有这个序列化器
class FooSerializer < ActiveModel::Serializer
attributes :this, :that, :the_other
def this
SomeThing.expensive(this)
end
def that
SomeThing.expensive(that)
end
def the_other
SomeThing.expensive(the_other)
end
end
Run Code Online (Sandbox Code Playgroud)
单个序列化值的操作有些昂贵...
然后我有另一个序列化器,它可以使用它,但不会返回所有成员:
class BarSerializer < FooSerializr
attributes :the_other
end
Run Code Online (Sandbox Code Playgroud)
这不起作用...... BarSerializer仍然会有这个,那个和另外的......
如何使用继承但不能自动获取相同的属性?我正在寻找除模块mixins之外的解决方案.
json ×5
ember.js ×2
activemodel ×1
api ×1
associations ×1
pagination ×1
performance ×1
ruby ×1