ruby on rails - Acts_as_paranoid and validation



使用rails 3和acts_as_paranoid。我想确保每个任务都有评论。

我怎么能绕过这一个验证(check_if_notes)如果mark_completed_and_msg被调用?

这是正确的方式吗?

task.rb

has_one :comment, :as => :commentable
attr_accessor :force_task
# original was - before_update, :check_if_notes
validate :check_if_notes, :on => :update, :unless => proc { |a| a.force_task }
def mark_completed_and_msg(user_id, msg)
   Comment.create!(:commentable_type => self.class, :commentable_id => self.id, :content => msg )
   self.update_attributes(:completed_by => user_id, :deleted_at => Time.now, :force_task => true)
end
def check_if_notes
  if self.comment.blank? || self.comment.content.blank? || self.comment.content.scan(/[w-]+/).size <= 2
    saved = false
    self.comment.errors[:content] << "You must have a comment and more than 3 words."
    raise ActiveRecord::Rollback
  end
end

认为您的意思是在您的程序中使用==

:unless => proc { |a| a[:force_task] == true }

也可以使用

:unless => proc { |a| a.force_task? }

相关内容

  • 没有找到相关文章

最新更新