Acts_as_paranoid和验证

时间:2011-04-15 23:46:42

标签: ruby-on-rails ruby-on-rails-3

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

如果调用mark_completed_and_msg,我如何绕过这一验证(check_if_notes)?

编辑 - 这是正确的方法吗?

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

1 个答案:

答案 0 :(得分:0)

认为你的意思是在你的过程中使用==

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

您也可以使用

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