No one was able to answer kris's question:

how to get validates_presence_of to work with non-null float/boolean/integer fields?

Has anyone figured out a way to get validates_presence_of to work with
float, boolean, and integer fields that are defined as non-null in the
schema?

The problem is, rails intializes these types to the default schema value,
which for mysql is never null if the column is not allowed to be null,
and as a result they are initialized in the form with a value, and are
never null.

I found a work around for integers that requires a nasty method override
in the ActiveRecord class that sets the default values from the
database, but I can not find a solution to do the same for floats
because ruby will not let me assign a float to null once it has been
initialized with a value.

People succeed in answering kris's questions 14% of the time (1 success in 7 attempts).

Answers by: Don Miguel de los Platanos | Rich Collins

Don Miguel de los Platanos's Answer:

Reply by Don Miguel de los Platanos 827 days ago

Well the method (validates__presence__of) is working fine because if you setup the values to be initialized via the schema settings, they are indeed seeded and present. So why not just set the schema to be null like Rich suggested?

Reply by kris 827 days ago

Because then I can not be sure that the data in the database is valid. A row should not be saved to the database unless these values are set. The database schema defination should enforce data integretiy, because in my case if the data goes bad so does everything else.

I am not setting up the values to be initialized, they just happen to be because mysql does not allow me to specifiy a null default value for these field types and rails is not smart enough to read my migrations to see what the real default value should be.

If there is no way around this then I believe this is a bug in rails.

Reply by Don Miguel de los Platanos 827 days ago

I guess its just a design philosophy issue because I do not see how this is at all a bug. The method is simply verifying something other then null is in the field, this is the correct behavior. The method name says it all, there is some value present in the field - regardless if its a default value or not. Its present.

I prefer to rely on my code to make sure that the proper values are initialized before there are saved to the db. All sorts of things can be used to ensure this, like using hooks to check values after something has been created. Using transactions and rolling back. While I see your reasons for relying on mysql to make sure null values don't make it into your db, its not at all necessary.

Reply by Don Miguel de los Platanos 827 days ago

protected
def validate
errors.add(:float, "Cannot be null") if float = 0.00
end

Reply by Don Miguel de los Platanos 827 days ago

Try adding something like that to the model.

Reply by Don Miguel de los Platanos 827 days ago

*float == 0.0

Reply by kris 824 days ago

Hi Miguel,

I appreciate your ideas, but I am only looking for a way to actually make things work as I stated in the original question... That is to detect when they are NIL.

I can't just check if the float is 0 because 0 is a valid value, and that doesn't work for booleans either.

Thanks anyways, I suspect there isn't a solution to this problem.