我正在使用主动模型序列化器.我有一个有很多活动的模特活动.
我想用前n个活动返回事件.我想我应该将params n传递给事件序列化器.
我有一个序列化器
class FundingSerializer < ActiveModel::Serializer
attributes :id,
has_one :user
has_one :tournament
embed :ids, include: true
end
Run Code Online (Sandbox Code Playgroud)
用适当的关联初始化
FundingSerializer.new(Funding.first).to_json
Run Code Online (Sandbox Code Playgroud)
产量
"{\"users\":[{\"id\":2,\"first_name\":\"Nick\"}],\"tournaments\":[{\"id\":1,\"end_date\":\"2013-07-21T23:18:54.981Z\",\"start_date\":\"2013-07-14T23:18:54.980Z\"}],\"funding\":{\"id\":1}}"
Run Code Online (Sandbox Code Playgroud)
但,
FundingSerializer.new(Funding.all).to_json
Run Code Online (Sandbox Code Playgroud)
得到这个错误.
undefined method `read_attribute_for_serialization' for #<ActiveRecord::Relation::ActiveRecord_Relation_Funding:0x007f998910a250>
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:121:in `method_missing'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:68:in `method_missing'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:99:in `block in attribute'
from (eval):3:in `_fast_attributes'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:466:in `rescue in attributes'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:454:in `attributes'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:478:in `_serializable_hash'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:360:in `serializable_hash'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:344:in `as_json'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:50:in `block in encode'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:49:in `encode'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:34:in `encode'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:333:in `to_json'
from …Run Code Online (Sandbox Code Playgroud) 如果某些条件为真,我该如何渲染属性?
例如,我想在create action上呈现User的token属性.
我已经开始了Rails 4.
在处理JSON格式数据时,我发现我们可以使用rails/jbuilder并且运行良好.
但是,当我拿着Codeschool的Rails 4 Pattern时,他们提到了gem active_model_serializers.
对于active_model_serializersgem而言,所有JSON序列化逻辑都会进入Model(被认为是最佳实践).
而对于jbuildergem,我们需要编写带扩展名的单独视图文件.json.jbuilder.
我的问题是:
ruby ruby-on-rails jbuilder active-model-serializers ruby-on-rails-4
我正在使用Rails 4.2.1和active_model_serializers 0.10.0.rc2
我是API的新手并选择active_model_serializers因为它似乎成为rails的标准(虽然我不反对使用RABL或其他串行器)
我遇到的问题是我似乎无法在多级关系中包含各种属性.比如我有:
项目
class ProjectSerializer < ActiveModel::Serializer
attributes :id,
:name,
:updated_at
has_many :estimates, include_nested_associations: true
end
Run Code Online (Sandbox Code Playgroud)
和估计
class EstimateSerializer < ActiveModel::Serializer
attributes :id,
:name,
:release_version,
:exchange_rate,
:updated_at,
:project_id,
:project_code_id,
:tax_type_id
belongs_to :project
belongs_to :project_code
belongs_to :tax_type
has_many :proposals
end
Run Code Online (Sandbox Code Playgroud)
建议
class ProposalSerializer < ActiveModel::Serializer
attributes :id,
:name,
:updated_at,
:estimate_id
belongs_to :estimate
end
Run Code Online (Sandbox Code Playgroud)
当我点击/projects/1上面产生的时候:
{
"id": 1,
"name": "123 Park Ave.",
"updated_at": "2015-08-09T02:36:23.950Z",
"estimates": [
{
"id": 1, …Run Code Online (Sandbox Code Playgroud) 我有一个模型对象,它不是ActiveRecord :: Base的后代,也不存储在数据库中.我为它创建了一个序列化程序(使用相同的名称+"Serializer"),并在我的控制器中调用render json: object_instance.
结果是来自内心深处的例外render.
我实现了一个as_json实例化序列化器并调用它的方法,但结果是对象中缺少的方法,read_attribute_for_serialization.
我希望我的对象至少就Active Model Serializer而言与ActiveModel兼容的对象一样,但我在他们的文档中没有看到任何对此的引用.
鉴于以下ActiveModel::Serializer课程:
class SampleSerializer < ActiveModel::Serializer
attributes :id, :name
end
Run Code Online (Sandbox Code Playgroud)
如何测试RSpec?
rspec ruby-on-rails active-model-serializers ruby-on-rails-4
这个问题与AMS 0.8有关
我有两个型号:
class Subject < ActiveRecord::Base
has_many :user_combinations
has_ancestry
end
class UserCombination < ActiveRecord::Base
belongs_to :stage
belongs_to :subject
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
两个序列化器:
class UserCombinationSerializer < ActiveModel::Serializer
attributes :id
belongs_to :stage
belongs_to :subject
end
class SubjectSerializer < ActiveModel::Serializer
attributes :id, :name, :description, :subjects
def include_subjects?
object.is_root?
end
def subjects
object.subtree
end
end
Run Code Online (Sandbox Code Playgroud)
当UserCombination序列化时,我想嵌入整个主题子树.
当我尝试使用此设置时,我收到此错误:
undefined method `belongs_to' for UserCombinationSerializer:Class
Run Code Online (Sandbox Code Playgroud)
我试过改变UserCombinationSerializer这个:
class UserCombinationSerializer < ActiveModel::Serializer
attributes :id, :subject, :stage
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我没有错误,但是subject以错误的方式序列化 - 不使用SubjectSerializer. …
我收到错误:
<NoMethodError: undefined method `read_attribute_for_serialization' for #<Record::ActiveRecord_Relation:0x007faa7cfe3318>>
Run Code Online (Sandbox Code Playgroud)
该错误位于代码段的第三行.
def artist
@records = Record.where('artist_id = ' + params[:artist_id])
render :json => @records, serializer: RecordSummarySerializer
end
Run Code Online (Sandbox Code Playgroud)
这是控制器RecordsContrller的备用序列化程序.另一个,'RecordsSerializer,工作正常.
对此错误的搜索提出了一些解决方案.有些人不清楚添加代码的位置.另一位建议补充:
alias :read_attribute_for_serialization :send
Run Code Online (Sandbox Code Playgroud)
到发生错误的类.它对我不起作用.我还查看了gem文档.我找到了使用序列化器的一个例子:很明显,任何其他代码都是必需的.
我正在使用Ruby 2.3.0和Rails 5.0.0.
class RecordsController < ApplicationController
...
def index
@records = Record.order('title')
render :json => @records
end
def artist
@records = Record.where('artist_id = ' + params[:artist_id])
render :json => @records, serializer: RecordSummarySerializer
end
...
end
Run Code Online (Sandbox Code Playgroud)
class Record < ActiveRecord::Base
belongs_to :artist
belongs_to :label
belongs_to :style
has_many :tracks …Run Code Online (Sandbox Code Playgroud) 我知道这个版本还没有正式发布但我今天检查了rc3,我注意到我不能再在我的序列化器中使用Rails url helpers.在0.8.x版本中,我可以执行以下操作:
class BrandSerializer < BaseSerializer
attributes :id, :name, :slug, :state
attributes :_links
def _links
{
self: api_v1_company_brand_path(object.company_id, object.id),
company: api_v1_company_path(object.company_id),
products: api_v1_company_brand_products_path(object.company_id, object.id)
}
end
end
Run Code Online (Sandbox Code Playgroud)
但这在新版本中是不可取的.解决这个问题的最佳方法是什么,以便我可以在链接器中保存链接?
编辑:现在我正在做以下事情但很想听听是否有更惯用的方式.
class BaseSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
Run Code Online (Sandbox Code Playgroud)