ruby-on-rails – Rails / Ember – active_model_serializer – 未定义的方法`object’

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails / Ember – active_model_serializer – 未定义的方法`object’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在active_model_serializer中为Ember应用程序加载数据,并在尝试包含对象时获取NoMethodError:

undefined method `object’ for #Email:0x00000100d33d20

它只发生在:include =>真的是这样设置的:

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

提前致谢.

解决方法

像上面提到的Deefour一样,确保你有一个序列化程序用于任何侧面加载的对象.在这种情况下,创建EmailSerializer:
class EmailSerializer < ActiveModel::Serializer
  attributes :id,:email_address
end

猜你在找的Ruby相关文章