我是一个新手入门。今天我遇到了一个保存相关模型的问题。rails activerecord保存有唯一性验证的关联模型
我有以下关联的2个模型,标记模型具有属性“名称”的验证角色为唯一性。
class Product < ActiveRecord::Base
has_and_belongs_to_many :tags
validates_associated :tags
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :products
validates :name, :presence => true, :uniqueness => true
end
我想存档的是创建一个产品对象与几个标签的关联。我用下面的功能:
def self.create_with_tags(value)
tags = []
if value.has_key? :tags
tags = value[:tags]
value.delete :tags
end
p = Product.new(value)
tags.each do |tag|
p.tags.build(:name => tag)
end
p.save!
p
end
测试代码是
p = Product.create_with_tags(:name => 'test product', :status => true, :tags =>['tag1','tag2','tag3','tag4'])
测试代码工作正常时,标签名称[ 'TAG1', '标签2', '标签3', 'TAG4']在数据库中不存在; 如果其中一个标签已经存在于数据库中,例如'tag1',那么关联验证将失败并且整个创建过程都会回滚。
我想要归档的是:如果数据库中已经存在一些标记,则验证不会失败,而是找到现有标记(但未创建),并创建产品标记与现有标记之间的关联。例如,如果数据库中已有'tag1',它将不会再次创建,但会创建products_tags表中的关联。
参照这个帖子找到最佳答案://stackoverflow.com/questions/4730493/rails-has-and-belongs-to-many-habtm-create-association-without-creating-ot – fuyi 2013-04-27 20:06:55