Ruby on Rails Friday, March 1, 2013


On Friday, March 1, 2013 2:58:53 PM UTC, Ruby-Forum.com User wrote:

> with the :if option
>
> validates :question_type,     :presence  => true
> validates :question,        :presence   => true, :if =>
> :standard_question?
>
> will call the standard_question? method and only enforce the presence
> validation on question if the method returns true. There is also a
> lambda
> form
>
> validates :question,        :presence   => true, :if => lambda {|record|
> record.question_type == 'standard'}
>
> Fred

Thanks Fred,
Got this error
undefined method `standard_question?' for #<Question:0x007f947ddb2e00>
Do I need to write a  standard_question method

Yes - it's up to you to write a standard_question? method. The lambda saves you having to have the condition in a separate method but can make your validation look more cluttered (especially for more complicated conditions).

Fred 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/sgk504Zwsz4J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment