0
我有两个模型用户和食谱。用户可以创建配方,用户可以将配方添加到收藏夹。协会在烬js
在rails中,我在User模型中使用了收藏夹模型(user_id,recipe_id)中的这种关联。 我已经有了最爱的用户食谱。我如何获得当前用户创建的食谱?
has_many :favorites
has_many :recipes
has_many :recipesbox, through: :favorites, source: :recipe
当我试图在Ember js中做这件事时,我有麻烦。
user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
has_many :recipes, embed: :ids
has_many :recipesbox, through: :favorites, embed: :ids
end
user.js.coffee
App.User = DS.Model.extend
name: DS.attr('string')
email: DS.attr('string')
recipes: DS.hasMany('App.Recipe')
recipe.js.coffee
App.Recipe = DS.Model.extend
user: DS.belongsTo('App.User')
title: DS.attr('string')
user.hbs
我的连载用户:
{
user: {
id: 1,
name: "ejiqpep",
email: "[email protected]",
recipe_ids: [
4,
5
],
recipesbox_ids: [
4,
1,
2,
3
]
}
}
它究竟是什么不起作用?用你当前的问题,我只能猜测并建议你将'has_many:recipesbox',通过':UserSerializer'中的:: favorites,embed :: ids更改为'has_many:favorites,通过:: favorites,embed :: id(只是为了避免现在的复杂性问题),并在您的User模型中添加收藏夹:DS.hasMany('App.Recipe')。另外,您可能需要在'UserSerializer'的'has_many'行中添加':include => true',具体取决于您想要做什么。 –
问题是我不知道如何在user.js.coffee中获得更多的has_many作为收藏夹。 – EJIqpEP7
怎么样:'收藏夹:DS.hasMany('App.Favorite')',然后分别模型'App.Favorite = DS.Model.extend({..});' – intuitivepixel