【发布时间】:2012-12-31 23:20:25
【问题描述】:
我正在尝试为 Ember 应用程序在 active_model_serializer 中旁加载数据,并在尝试包含对象时得到 NoMethodError:
#Email: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
提前致谢。
【问题讨论】:
-
你的
EmailSerializer在哪里?如果您尝试嵌入Email实例的序列化版本,我很确定您需要Email来拥有关联的序列化程序。object仅存在于扩展ActiveModel::Serializer的类中。 -
你是对的;我一直在等待问题超时来回答这个问题,以防其他人有问题。谢谢!
标签: ruby-on-rails ember.js active-model-serializers