所以我有两个 ActiveRecord 类
class User < ActiveRecord::Base
has_many :buyer_deals, :class_name => "Deal", :foreign_key => :buyer_id
has_many :seller_deals, :class_name => "Deal", :foreign_key => :seller_id
validates_presence_of :name # THIS SHOULD ONLY BE RUN IF USER IS A SELLER
# IN THE DEAL
validates_presence_of :phone # THIS SHOULD ONLY BE RUN IF USER IS A BUYER
# IN THE DEAL
end
class Deal < ActiveRecord::Base
belongs_to :seller, :class_name => 'User'
belongs_to :buyer, :class_name => 'User'
validates_associated :seller
validates_associated :buyer
end
我想做的是创造一个新的交易。
Deal.create(A NICE STRUCT WITH SELLER AND BUYER)
但是,如果交易的关系是卖家,我只想运行名称验证,如果交易的关系是卖家,我只想运行名称验证,这在 Rails 中是否可行,似乎在文档中找不到任何内容。