如何从嵌套模型中引用高级模型?

| 我正在尝试根据付款模型中的项目状态进行条件验证。例如,状态可以是\“ Talks \”或\“ Active \”。考虑以下结构,最好的方法是什么?
class Project < ActiveRecord::Base
  has_many :costs, :dependent => :destroy
  has_many :payments, :through => :costs, :dependent => :destroy
  accepts_nested_attributes_for :costs, :allow_destroy => true
end
class Cost < ActiveRecord::Base
  has_many :payments, :dependent => :destroy
  accepts_nested_attributes_for :payments, :allow_destroy => true
  belongs_to :project
end
class Payment < ActiveRecord::Base
  belongs_to :cost
  validates_presence_of :value1, :if => :new?
  validates_presence_of :value1, :if => :talks?
  validates_presence_of :value2, :if => :active?

  def new?
    # if controller action is new
  end
  def talks?
    # if project status is \"Talks\" (edit action)
  end
  def active?
    # if project status is \"Active\" (edit action)
  end
end
    
已邀请:
class Payment < ActiveRecord::Base
  belongs_to :cost
  has_one :project, :through => :cost
  validates_presence_of :value1, :if => :new?
  validates_presence_of :value1, :if => :talks?
  validates_presence_of :value2, :if => :active?

  def new?
    self.new_record?
  end
  def talks?
    project.status == \"talks\"
  end
  def active?
    project.status == \"active\"
  end
end
    

要回复问题请先登录注册