使用 update_attributes() 和 has_many :through 关联时出现重复记录
我不明白为什么它会生成重复的recruit_profiles_skills而不是更新。
class RecruitProfile < ActiveRecord::Base
has_many :skills, :through => :recruit_profiles_skills
has_many :recruit_profiles_skills, :dependent => :destroy
accepts_nested_attributes_for :recruit_profiles_skills, :allow_destroy => true
class Skill < ActiveRecord::Base
has_many :recruit_profiles, :through => :recruit_profiles_skills
has_many :recruit_profiles_skills, :dependent => :destroy
参数看起来像
"recruit_profile"=>{
"recruit_profiles_skills_attributes"=>[{"skill_id"=>"1", "level"=>"15"}]
}
然后我这样做
def update
@recruit_profile.update_attributes(params[:recruit_profile])
但是,这会创建重复的关联记录。为什么这不简单地更新!? 我可以使用验证来防止重复项,但它永远不会更新,因为它只想创建新记录,但新记录无效,因为它未通过验证。
I can't figure out why it's generating duplicate recruit_profiles_skills instead of updating.
class RecruitProfile < ActiveRecord::Base
has_many :skills, :through => :recruit_profiles_skills
has_many :recruit_profiles_skills, :dependent => :destroy
accepts_nested_attributes_for :recruit_profiles_skills, :allow_destroy => true
class Skill < ActiveRecord::Base
has_many :recruit_profiles, :through => :recruit_profiles_skills
has_many :recruit_profiles_skills, :dependent => :destroy
Params looks like
"recruit_profile"=>{
"recruit_profiles_skills_attributes"=>[{"skill_id"=>"1", "level"=>"15"}]
}
Then I do
def update
@recruit_profile.update_attributes(params[:recruit_profile])
But, this creates duplicate association records. Why does this not simply update!?
I can prevent the duplicates using validations, but then it never updates since it just wants to create a new record, but the new record is invalid because it fails the validation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在代码中解决此问题的方法是:
该段代码将检查 :skill_id 是否已设置。如果未设置,则需要删除该行。即使 :allow_destroy 设置为 true,删除条目的方法也是将“:_destroy => 1” key=> value 附加到哈希中。因此, :id 将出现,并且 :_destroy 将出现,因此 update_attributes 将删除它。
执行上述操作将允许创建(:id 不存在,但 :skill_id 存在)、更新(:id 存在且 :skill_id 存在)和销毁(:id 存在,但 :skill_id 不存在)。恕我直言,这不是它应该如何工作的,但是只需要额外的 1 行代码就可以完成工作(由于长度
而分成 3 行)。(注意:将 Skill_id 替换为中的任何其他参数仅当您使用具有多个属性的关联表时才需要这种方式,否则经典的 collection_ids = [#,#,#] 仍然适用于 has_many :through 关联。)
The way I solved this problem in code was to:
That bit of code will check to see if the :skill_id is set. If it is not set, then the row needs to be deleted. The way to delete entries, even if :allow_destroy is set to true, is to append a ":_destroy => 1" key=>value to the hash. Thus, the :id will be present, and a :_destroy will be present, so update_attributes will delete it.
Doing the above will allow for create (:id not present, but :skill_id present), update (:id present and :skill_id present) and destroy (:id present, but :skill_id not present). IMHO, that isn't how it should work, but the job gets done with only 1 extra line of code (split in to 3 lines because of length).
(NOTE: replace skill_id with whatever other parameter is also in your association table. This round about way is only needed if you're using association tables with multiple attributes. Otherwise, the classic collection_ids = [#,#,#] still works with has_many :through associations.)