0

有没有更简单的方法可以从表中返回关系行以访问存储在那里的数据?

我有两个使用 has_many :through 相关的模型,第三个模型设置为中间。我的模型由 User、Recipe 和 RecipeInfo 组成。

现在,为了访问为特定用户的食谱信息存储的数据,我正在使用与此类似的 Rails 查询

info = @user.recipeInfos.where("recipe_id=#{@recipe.id}")

我想知道是否有一种更简单的方法来访问用户食谱信息的这一行,而不是使用 .where() 方法。

编辑:

recipe = Recipe.find(params[:id])

user = current_user

current_usersessions[:user_id] = current_user用户登录的时间定义。

4

1 回答 1

0

您可以将这四行代码缩短为

info = current_user.recipeInfos.find_by_id(params[:id])

.

于 2012-06-10T01:18:59.650 回答