Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling custom validation methods in Rails

I just upgraded my rails to 2.3.4 and I noticed this with validations: Lets say I have a simple model Company which has a name. nothing to it. I want to run my own validation:

class Company < ActiveRecord::Base

  validate :something


  def something
    false
  end

end

saving the model actually works in this case. The same thing happens if i override validate() and return false. I noticed this in a more complex model where my validation was returning false, but the object was still saving...I tried it out in an essentially empty model and the same thing applied. Is there a new practice I am missing? This doesn't seem to be the case in some of my older rails code.

like image 853
cgr Avatar asked Oct 23 '09 07:10

cgr


People also ask

What is validation in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.

What is Active Record in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

What is Validates_presence_of?

validates_presence_of(*attr_names) public. Validates that the specified attributes are not blank (as defined by Object#blank?), and, if the attribute is an association, that the associated object is not marked for destruction. Happens by default on save.


3 Answers

Your validations are executed when you use the validate method. However rails doesn't relies on the returned value.

It relies on if there are validations errors or not. So you should add errors when your model doesn't validates.

def something
    errors.add(:field, 'error message')
end

Or, if the error is not related to a field :

def something
    errors.add(:base, 'error message')
end

Then your model won't be saved because there are errors.

like image 133
Damien MATHIEU Avatar answered Oct 10 '22 18:10

Damien MATHIEU


You're getting confused between validations and callbacks.

Validations are supposed to fail if there are any errors on the object, doesn't matter what the validation returns. Callbacks fail if they return false, regardless if they add any errors to object.

Rails uses calls valid? from save calls which does not check the result of any validations.

Edit: Rails treats validate :method as a callback, but valid? still doesn't check for their results, only for errors they added to the object.

I don't think this behaviour changed at all but I could be wrong. I don't think I've ever written a validation to return false before.

like image 41
EmFi Avatar answered Oct 10 '22 19:10

EmFi


Just FYI errors.add_to_base('error message') has been deprecated in rails 3 and got replaced by

errors[:base] << "Error message" 

Or

errors.add(:base, "Error message")
like image 24
Maged Makled Avatar answered Oct 10 '22 18:10

Maged Makled