【发布时间】:2015-09-07 13:17:12
【问题描述】:
我一直在使用这个带有 has_many 的嵌套属性:通过
class Check < ActiveRecord::Base
has_many :checks_tags, dependent: :destroy
has_many :tags, through: :checks_tags
attr_accessible :tags_attributes, :checks_tags_attributes
accepts_nested_attributes_for :tags, :checks_tags
end
class Tag < ActiveRecord::Base
has_many :checks_tags, dependent: :destroy
has_many :checks, through: :checks_tags
end
class CheckTag < ActiveRecord::Base
belongs_to :check
belongs_to :tag
end
所以这里的问题是当我用这个哈希创建时
"tags_attributes"=>[{"id"=>"", "name"=>"test12", "company_id"=>"1"}, {"id"=>"", "name"=>"test12", "company_id"=>"1"}]
实际上这里有两个同名的标签,所以它创建了两次Tag,然后在CheckTag上放了两次,所以有什么办法可以避免这种创建两次标记?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 rails-activerecord