开发者

Acts_as_paranoid and validation

开发者 https://www.devze.com 2023-02-26 19:59 出处:网络
Using rails 3 and acts_as_paranoid.I want to make sure that every task has a comment. How can I bypass this one validation (check_if_notes) if mark_completed_and_msg is called?

Using rails 3 and acts_as_paranoid. I want to make sure that every task has a comment.

How can I bypass this one validation (check_if_notes) if mark_completed_and_msg is called?

EDIT - Is this the right way?

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 => pro开发者_运维知识库c { |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


I think you meant using == in your Proc.

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

You can also use

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

精彩评论

暂无评论...
验证码 换一张
取 消