我试图在active_model_serializer中为Ember应用程序加载数据,并在尝试包含对象时获取NoMethodError:
@H_301_3@undefined method `object’ for #Email:0x00000100d33d20@H_301_3@它只发生在:include =>真的是这样设置的:
class ContactSerializer < ActiveModel::Serializer embed :ids,:include => true attributes :first_name,:last_name has_many :emails end@H_301_3@我的模型看起来像这样:
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@H_301_3@我的控制器如下所示:
def show @contact = @current_user.contacts.where(:id => params[:id]).includes(:emails).first render :json => @contact end@H_301_3@提前致谢.
解决方法
像上面提到的Deefour一样,确保你有一个序列化程序用于任何侧面加载的对象.在这种情况下,创建EmailSerializer:
class EmailSerializer < ActiveModel::Serializer attributes :id,:email_address end