我有两个模型Users和Recipes。用户可以创建食谱,用户可以将食谱添加到收藏夹。
在 Rails 中,我在用户模型中与收藏夹模型(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
用户.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')
用户.hbs
<h1>{{name}}</h1>
<p><b>Email:</b> {{email}}</p>
<h3>Favorites</h3>
{{#each recipe in recipes}}
<div>
<span>{{recipe.id}}</span>
<span>{{recipe.title}}</span>
</div>
{{/each}}
我的序列化用户是:
{
user: {
id: 1,
name: "ejiqpep",
email: "ejiqpep@gmail.com",
recipe_ids: [
4,
5
],
recipesbox_ids: [
4,
1,
2,
3
]
}
}