【发布时间】:2014-11-05 04:31:27
【问题描述】:
我有一个 Post 模型和 Comment 模型(Post has_many cmets,Comment belongs_to Post)
我想验证我的评论模型中的一个字段,如果评论字段小于 2 个 Post date_fields 之间的差异,他们只能创建/更新。我该如何实现这一目标?
现在,在我的评论模型中,我有:
validates :comment_check, presence: true, :numericality => { :greater_than_or_equal_to => 0 }
我只需要添加验证以使其 :less_than_or_equal_to 发布日期差异。我的 Post 模型中也有这个模型方法:
def self.days_diff
(end_date.to_date - start_date.to_date).to_i
end
任何帮助将不胜感激!
【问题讨论】:
标签: ruby-on-rails ruby validation model associations