Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

validations based on methods and not actual attributes

#2545

The validations are calling methods. Let's say I have an attribute 'attr' and I have a validates_presence_of :attr. Now I have the following method

def attr super || 0 end

This is a dumb example, but I think you will get the point.

MyObject.create!

does not fail. This is because the validations are running against methods and not against actual attribute values.

Reported by Amos King · April 22nd, 2009 @ 11:43 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Max Lapshin
    Max Lapshin
    • Tag set to invalid

    It is ok, because You've defined accessor method yourself.

    April 23rd, 2009 @ 10:22 AM

  2. Amos King
    Amos King
    • Tag changed from invalid to validations

    It is not ok. If your database isn't constraining that column you may have bad data in the database.

    April 23rd, 2009 @ 01:25 PM

  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to wontfix

    If you have a fix for this, please submit a patch. Otherwise, it looks like a simple case of "don't do that." If you patch, please explain how Rails is supposed to figure out what database column you intend to be validated for an arbitrary method.

    April 23rd, 2009 @ 01:47 PM

  4. Amos King
    Amos King

    Rails should validate off of the attribute and save from the attribute. I'm not suggesting to change the save. The save currently works off of the attributes. I'm suggesting that since save is run from those attributes that validations should also be run against the same attributes.

    April 23rd, 2009 @ 03:18 PM