ruby on rails - ActiveRecord attribute_present? lies. How can I fix it? -


the docs attribute_present? say:

returns true if specified attribute has been set user or database load ...

but... that's not true! see here rails initializes attributes on new object database defaults.

so, suppose have users table age not null default 0.

user.new.attribute_present?(:age) == true

but hasn't been set or database load.

perhaps i'm arguing semantics, in case, i'd method says: tells me if field has been explicitly set.

e.g.

u = user.new # u.attribute_set?(:age) == false u.age = u.age #set explicitly default, example # u.attribute_set?(:age) == true 

does exist?

as far know, there isn't way (provided have default arguments in db) (as design)

edited: edited original answer based on z5h's comments below