Using question mark character in Rails/ActiveRecord column name
Solution 1
Rails will automatically generate the method smart?
if there is a field named 'smart'
.
Solution 2
One "gotcha" to be aware of if you happen to use :enum
in your model, since this stores the value as an integer. The question mark attr method provided by active record expects to evaluate 0 or 1 as false / true respectively in the database. For example:
class Person
enum mood: ['happy', 'sad', 'bored']
end
p = Person.new(mood: 'happy') # this would store mood as 0 in db
p.mood? #=> false
p.mood = 'sad' # saves as 1 in db
p.mood? #=> true
p.mood = 'bored' # saves as 2 in db
p.mood? #=> true
to see how this method works, see rails source
at.
Updated on June 06, 2022Comments
-
at. about 2 years
In keeping with Ruby's idiom of using a question mark in boolean methods (e.g.
person.is_smart?
), I'd like to do the same for an ActiveRecord field in Rails:rails generate model Person is_smart?:boolean
I haven't actually run the above statement. I assume that database fields can't have a question mark in them. Will rails deal with this appropriately? Is the best practice to simply leave question marks off of models?
Using Rails 3.2.8
-
matt almost 12 yearsWhy not just call it
smart
and then add custom accessors so you can sayis_smart?
when you're talking to an actual Person? -
at. almost 12 years@matt - I guess that makes the most sense since it's not common that you would name a boolean variable with a question mark at the end and we would be treating the above
is_smart?
as a variable. -
oldergod almost 12 yearsRails is your friend. See Cdesrosiers' answer.
-
-
Kelvin about 11 yearsActually Rails generates
?
methods for all columns. They appear to return true only if the value is truthy. So this is a perfect match for booleans. -
MCB over 10 yearsI made an "ask/ answer" post on this topic after this answer, but I kept this as I think this information might be valuable for deciding how to name a bool column as well.
-
Kyle Heironimus almost 8 yearsActually, Kelvin's comment above is almost right. It returns true if the value is
present?
, not truthy. So "" returns false, for example. -
mwfearnley over 7 yearsIs there any documentation for the semantics of the question-mark form?
-
dre-hh about 7 yearsnever have been i so wrong. also thought it would just check for
present?
github.com/rails/rails/blob/master/activerecord/lib/… -
alex almost 7 years...and remember: it is a bad practice in rails to set prefixes like 'is_' or 'has_' in yours methods and columns stackoverflow.com/questions/37059547/…
-
alex almost 7 years..and, until is a bad practice, actually it is possible at least in PosgreSQL and MySQL, to use question marks in the columns names which is very painful to use when you write it in a statement.
-
vladpisanov about 5 yearsSurprisingly, for numeric attributes, the question mark helper checks for 0. If user.level is 0, then user.level? is false but user.level.present? is true!
-
illusionist about 4 yearsits not that you cant call boolean cols without question mark. Its actually showing the actual value you stored in the col. add
null: false, default: false
clause in the migration.