Ruby on rails 如何在用户id veritable上的每个记录中传递当前用户?轨道

Ruby on rails 如何在用户id veritable上的每个记录中传递当前用户?轨道,ruby-on-rails,ruby-on-rails-4,nested,associations,nested-forms,Ruby On Rails,Ruby On Rails 4,Nested,Associations,Nested Forms,我想将user\u id从当前用户传递到嵌套表单中的每个关联 def create @entity = Entity.new(entity_params.merge(user: current_user)) # entity has_many :boxes and in form i'm making them more than one @entity.boxes.user_id = current_user.id if current_user # e

我想将
user\u id
从当前用户传递到嵌套表单中的每个关联

def create
    @entity = Entity.new(entity_params.merge(user: current_user)) 

    # entity has_many :boxes and in form i'm making them more than one
    @entity.boxes.user_id = current_user.id if current_user 

    # entity has_many :orders and in form i'm making them more than one
    @entity.orders.user_id = current_user.id if current_user
end 
我犯了这个错误

未定义的方法“user\u id=”


我需要一种方法,我如何给每个
订单
user\u id=当前用户。数据库中的所有表都有user\u id列,每个模型都有
属于:user
属于
类型,即您必须对它们进行迭代,以获得具有相关
user
的单个项目
box
order

@entity.boxes.each do |box|
  box.user_id = current_user.id if current_user.present?
end
如果要保存对数据库的更改,还可以使用:

box.update!(user: current_user) if current_user.present?

另外,我假设
用户
有很多
:框
:orders
属于
类型,也就是说,您必须对它们进行迭代,以获得包含相关
用户的单个项目
订单

@entity.boxes.each do |box|
  box.user_id = current_user.id if current_user.present?
end
如果要保存对数据库的更改,还可以使用:

box.update!(user: current_user) if current_user.present?

另外,我假设
用户
有很多
:box
:orders

我添加了额外的行
box.paper。每个都…
并且它通过关联也为很多人工作!谢谢,我增加了额外的一行
box.paper.each do…
,它也通过关联为很多人工作!谢谢