我试图找出如何有条件地关联2个表,根据用户选择哪一个选项。例如:
模型店属于商店类中的商店类,有三种选择,即复古、独立标签和零售商。如果用户选择独立的标签,就会与独立的品牌模式相关联。
发布于 2014-06-01 20:52:50
你可以像正常一样模拟这种联系。然后,您可以使用验证来确保independent_brand是否被适当地应用。就像这样:
class IndependentBrand < ActiveRecord::Base
...
end
class Shop < ActiveRecord::Base
belongs_to :independent_brand
validates :independent_brand, {
:presence => {if: :independent_brand?},
:absence => {unless: :independent_brand?}
}
def independent_brand?
self.kind == "independent_brand"
end
endhttps://stackoverflow.com/questions/23983446
复制相似问题