Rails/Ember - active_model_serializer - sideloading时未定义的方法`object'

use*_*736 13 ruby-on-rails ember.js 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)

提前致谢.

use*_*736 26

正如上面提到的Deefour,请确保您有任何侧载对象的序列化程序.在这种情况下,创建EmailSerializer:

class EmailSerializer < ActiveModel::Serializer
  attributes :id, :email_address
end
Run Code Online (Sandbox Code Playgroud)