如何从嵌套模型引用高级模型?
我正在尝试根据付款模型中的项目状态进行条件验证。例如,状态可以是“对话”或“活动”。考虑到下面的结构,最好的方法是什么?
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
I'm trying to make conditional validation based on Project status in Payment model. For example status can be "Talks" or "Active". What's the best way to do it considering the structure below?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)