4

i'v been new to ruby and rails and encountered a rather strange error:

class Person < ActiveRecord::Base
    validates_presence_of :description, :if => require_description_presence?

    def require_description_presence?
        self.can_send_email
    end
end

raises

NoMethodError in PeopleController#index

undefined method `require_description_presence?' for #<Class:0x4c4fadc> 

2 Answers 2

8

You should pass validation method as symbol:

validates_presence_of :description, :if => :require_description_presence?
Sign up to request clarification or add additional context in comments.

Comments

0

You can make this even shorter and sweeter. The :if clause will take an attribute just as easily. So if can_send_email is a boolean attribute of Person, this will work:

class Person < ActiveRecord::Base
  validates_presence_of :description, :if => :can_send_email?
end

No need to create another method just to check this attribute. And if you notice the extra question mark at the end of can_send_email, it's because Rails lets you do that with boolean attributes. I like it because it makes the code's purpose more clear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.