我正在尝试为ember应用程序在active_model_serializer中加载数据,并在尝试包含对象时获取NoMethodError:
#Email的未定义方法`object':0x00000100d33d20
只有在以下情况下才会发生:include =>设置为true,如下所示:
class ContactSerializer < ActiveModel::Serializer
embed :ids, :include => true
attributes :first_name, :last_name
has_many :emails
end
我的模特看起来像这样:
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
我的控制器看起来像这样:
def show
@contact = @current_user.contacts.where(:id => params[:id]).includes(:emails).first
render :json => @contact
end
提前致谢。
答案 0 :(得分:26)
正如上面提到的Deefour,请确保您有任何侧载对象的序列化程序。在这种情况下,创建EmailSerializer:
class EmailSerializer < ActiveModel::Serializer
attributes :id, :email_address
end