为什么这个关联的创建调用不起作用?

时间:2021-11-23 12:44:39

I've got a User model that has many Items. A Rating belongs to a User and an Item.

我有一个有很多项目的用户模型。评级属于用户和项目。

In the DB, I have set ratings.user_id to be not NULL.

在DB中,我将ratings.user_id设置为非NULL。

when I am creating an Item, I would like to do this:

当我创建一个Item时,我想这样做:

  def create
    current_user.items.create(params[:item]).ratings.create(params[:rating]
    redirect_to items_path
  end

However, this balks with an SQL error "user_id cannot be nil"

但是,这个带有SQL错误“user_id不能为零”的错误

so I rewrote the create method as

所以我重写了create方法

  def create
    current_user.items.create(params[:item]).ratings.create(params[:rating].merge({:user_id => current_user}))
    redirect_to items_path
  end 

which works fine.

哪个工作正常。

However, I had thought that chaining the create methods off the current user's receiver would have populated the rating's user_id. Anyone know why not?

但是,我原本以为将当前用户接收器的创建方法链接起来就会填充评级的user_id。谁知道为什么不呢?

TIA.

3 个解决方案

#1


I'd recommend you normalize this if possible in the database. Maybe take out the user_id attribute from the ratings table and if you need it in your model get it through a join using a :through method

我建议你在数据库中尽可能规范化。也许从评级表中取出user_id属性,如果在模型中需要它,可以使用:through方法通过连接获取它

class Rating
    has_many :items
    has_one :user, :through=>:items

#2


If you created and saved the Item, then made a Rating from that item, it wouldn't pass the user along to the Rating, right? You'd just refer to it as @rating.item.user, right?

如果您创建并保存了该项目,然后从该项目进行评级,则不会将用户传递给评级,对吧?你只需将它称为@ rating.item.user,对吗?

When you think about it like that, you wouldn't expect the Item created via the current_user to pass the user information along to the rating.

当你这样想时,你不会期望通过current_user创建的Item将用户信息传递给评级。

Make me wonder if you really need the user has_many ratings relationship.

让我想知道你是否真的需要用户has_many评级关系。

#3


Because Item has many Ratings and that association does not know about the user id. Given that association chain Item would have a user id because it belongs to a user. And Rating would have an item id because it belongs to an item. But the Item to Rating assocation doesn't know anything about a user unless you tell it.

因为Item有很多评级,并且该关联不知道用户ID。鉴于该关联链项具有用户ID,因为它属于用户。评级会有一个项目ID,因为它属于一个项目。但是除非你告诉它,否则物品到评级关联对用户一无所知。

#1


I'd recommend you normalize this if possible in the database. Maybe take out the user_id attribute from the ratings table and if you need it in your model get it through a join using a :through method

我建议你在数据库中尽可能规范化。也许从评级表中取出user_id属性,如果在模型中需要它,可以使用:through方法通过连接获取它

class Rating
    has_many :items
    has_one :user, :through=>:items

#2


If you created and saved the Item, then made a Rating from that item, it wouldn't pass the user along to the Rating, right? You'd just refer to it as @rating.item.user, right?

如果您创建并保存了该项目,然后从该项目进行评级,则不会将用户传递给评级,对吧?你只需将它称为@ rating.item.user,对吗?

When you think about it like that, you wouldn't expect the Item created via the current_user to pass the user information along to the rating.

当你这样想时,你不会期望通过current_user创建的Item将用户信息传递给评级。

Make me wonder if you really need the user has_many ratings relationship.

让我想知道你是否真的需要用户has_many评级关系。

#3


Because Item has many Ratings and that association does not know about the user id. Given that association chain Item would have a user id because it belongs to a user. And Rating would have an item id because it belongs to an item. But the Item to Rating assocation doesn't know anything about a user unless you tell it.

因为Item有很多评级,并且该关联不知道用户ID。鉴于该关联链项具有用户ID,因为它属于用户。评级会有一个项目ID,因为它属于一个项目。但是除非你告诉它,否则物品到评级关联对用户一无所知。