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…
,它也通过关联为很多人工作!谢谢