我有两个模型User
和Investment
和一个polymorhic模型Address
如何将验证范围限定在多态关联中的特定模型。
class User < ActiveRecord::Base
has_one :address, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :address
end
class Investment < ActiveRecord::Base
has_many :addresses, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :addresses, reject_if: lambda { |v| v['address'].blank? } && :address_blank, :allow_destroy => true
end
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
validates :address, presence: true
end
现在validates :address, presence: true
将同时适用于Investment
以及User
,但我希望它仅适用于Investment
不User
。所以我该怎么做。
谢谢。
多态,我的投资模式没有investment_id, –