validates_presence_of question

I'm still new to Rails, but I think you need to add a 'validate' method to your controller and perform the check in there instead.

Off the top of my head, something like:

protected def validate   errors.add("Either tweedledum or tweedledee need to be entered.") unless tweedledum != nil or tweedledee != nil end

I can't see a way of using validates_presence_of for this.

Regards,

Chris.

I think Chris meant to tell you to add the validate method to the model. This will then be called in the normal validation routine.

Mark

Thanks, Chris! And is it possible to embed this 'validate' method into the model; say, into the before_save filter or something like that?

If you have a method called "validate" on your model, this is used automatically by the valid? method. No need to do anything else.

Max

Hi !

If you have a method called "validate" on your model, this is used automatically by the valid? method. No need to do anything else.

The only problem with that is if you have subclasses that override the validate method. Unless the subclass calls the superclass method, your validations won't work as expected.

class Order < AR::Base   def validate     # this will never be called   end end

class RushOrder < Order   def validate     self.errors.add(:status, 'invalid') if self.status.blank?   end end

If you do this instead:

class Order < AR::Base   before_validation :check_base_order   def check_base_order     # this will always be called   end end

class RushOrder < Order   before_validation :check_rush_order   def check_rush_order     # this will ALSO always be called   end end

Hope that helps !

Yes, I meant model, but wrote controller. Also it should be @tweedledum etc. The 'unless' is a continuation of the 'errors.add' line, but it's come out with bad formatting.

Regards,

Chris.