[英]rails: has_and_belongs_to_many not saving associated objects (null in join table)
Rails 4. I got an Authentication
model Rails 4.我有一个
Authentication
模型
class Authentication < ActiveRecord::Base
...
has_and_belongs_to_many :posting_groups
...
end
and a PostingGroup
model 和
PostingGroup
模型
class PostingGroup < ActiveRecord::Base
...
has_and_belongs_to_many :authentications
...
end
The migration for the join table look like this: 联接表的迁移如下所示:
def change
create_table :authentications_posting_groups, id: false do |t|
t.belongs_to :posting_group
t.belongs_to :authentication
end
end
Neither the Authentication
nor the PostingGroup
tables need references to each other or to the join table, so there's no relevant information in them. Authentication
表和PostingGroup
表都不需要互相引用,也不需要引用PostingGroup
表,因此它们之间没有任何相关信息。 When updating the PostingGroups
of an Authentication
I do the following: 更新
Authentication
的PostingGroups
,请执行以下操作:
def update
@authentication.update_attributes(authentication_params)
end
def authentication_params
params.require(:other_unimportant_stuff, posting_groups: [])
end
During the execution of the update
action, params
is as follows: 在执行
update
操作期间, params
如下:
{"authentication"=>{"posting_groups"=>["17", "18"]}, "stuff"=>"more stuff}
{“ authentication” => {“ posting_groups” => [“ 17”,“ 18”]},“ stuff” =>“更多内容}
...where, 17 and 18 are the ids of the posting groups which should be saved. ...其中17和18是应保存的发布组的ID。 And, indeed, the
@authentication
variable in update
contains the desired PostingGroups
and update_attributes
returns true. 并且的确,
update
中的@authentication
变量包含所需的PostingGroups
并且update_attributes
返回true。 However, Authentication.find(id).posting_groups
(ie what actually was stored in the db) returns an empty set. 但是,
Authentication.find(id).posting_groups
(即实际存储在数据库中的内容)将返回一个空集。 Additionally, checking the authentications_posting_groups
table shows the following: 此外,检查
authentications_posting_groups
表显示以下内容:
posting_group_id authentication_id
17 NULL
18 NULL
For some weird reason the authentication_id
is never making it to the db, which is funny because the save was triggered on this object in the first place. 出于某些奇怪的原因,
authentication_id
从未进入数据库,这很有趣,因为首先是在此对象上触发了保存。 It's not the first time I encounter this problem but rather the second, the first time we noticed that everything worked fine (that is, those NULL
didn't pop up in the db) when creating what in this scenario is the Authentication
, but the same happened on update
. 这不是我第一次遇到此问题,而是第二次,我们第一次注意到在这种情况下创建
Authentication
时,一切工作正常(也就是说,那些NULL
没有在数据库中弹出),但是update
也发生了同样的事情。 On that occasion a hack was implemented re-creating the object (on create
everything is OK!!), but here that's not an option. 在那种情况下,实现了重新创建对象的hack(
create
一切正常!),但这不是一个选择。 I'd immensely appreciate any pointers because this is driving us mad. 我非常感谢您提出任何建议,因为这使我们发疯。 Thanks.
谢谢。
Edit - here's the form which is triggering the whole thing: 编辑-这是触发整个事件的表单:
= form_for authentication, remote: true do |f|
= f.select :posting_groups, options_for_select(@posting_groups.map {|pg| [ pg.name, pg.id ] }), {}, multiple: true, class: "form-control posting_groups_select"
You should permit posting_group_ids and pass posting_group_ids 您应该允许posting_group_ids并通过posting_group_ids
Controller 调节器
def update
@authentication.update_attributes(authentication_params)
end
def authentication_params
params.require(:other_unimportant_stuff, posting_group_ids: [])
end
View 视图
= form_for authentication, remote: true do |f|
= f.select :posting_group_ids, options_for_select(@posting_groups.map {|pg| [ pg.name, pg.id ] }), {}, multiple: true, class: "form-control posting_groups_select"
params.require(:other_unimportant_stuff, posting_group_ids: [])
Notice the ids. 注意id。
You're passing in posting_group_ids
as per your params but your parameters are set to posting_groups
so strong parameters will filter it out. 您按照参数传递了
posting_group_ids
,但是您的参数设置为posting_groups
因此强大的参数会过滤掉它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.