我有兩個模型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, –