0
我想确保项目名称在组织内是唯一的。所以我在Item中使用了“validates_uniqueness_of:name,scope:[:organization]”。这不幸没有奏效。唯一性字段使用范围
错误(编辑):
> 1) Item
> Failure/Error: @item_1 = create(:item, :item_category => @item_cat_1)
>
> NoMethodError:
> undefined method `organization_id' for #<Item:0x00000002565840>
型号:
class Item < ActiveRecord::Base
belongs_to :item_category
has_one :organization, through: :item_category
validates_uniqueness_of :name, scope: [:organization]
end
class ItemCategory < ActiveRecord::Base
has_many :items
belongs_to :organization
end
class Organization < ActiveRecord::Base
has_many :item_categories
has_many :items, :through => item_categories
end
从理论上讲,正如我上面没有我可以使用该项目的,item_category协会(belongs_to的:item_category)为的organization_ID?
如果以上是不可能的。我想我可以在item和item_category中有一个organization_id。但是,我们如何验证item.organization_id始终等于item_category.organization_id(其关联)
是你的业务需求,使得所有项目必须在整个组织具有唯一的名称?在不同的ItemCategories中重复的名称会好吗? – messanjah
所有项目在整个组织中必须具有唯一的名称。另外,由于item_category属于组织,因此组织范围内不应有重复的项目名称。希望澄清事情 – user1438150