Using question mark character in Rails/ActiveRecord column name

14,883

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

Share:
14,883
at.
Author by

at.

Updated on June 06, 2022

Comments

  • at.
    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
      matt almost 12 years
      Why not just call it smart and then add custom accessors so you can say is_smart? when you're talking to an actual Person?
    • at.
      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
      oldergod almost 12 years
      Rails is your friend. See Cdesrosiers' answer.
  • Kelvin
    Kelvin about 11 years
    Actually 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
    MCB over 10 years
    I 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
    Kyle Heironimus almost 8 years
    Actually, Kelvin's comment above is almost right. It returns true if the value is present?, not truthy. So "" returns false, for example.
  • mwfearnley
    mwfearnley over 7 years
    Is there any documentation for the semantics of the question-mark form?
  • dre-hh
    dre-hh about 7 years
    never have been i so wrong. also thought it would just check for present? github.com/rails/rails/blob/master/activerecord/lib/…
  • alex
    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
    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
    vladpisanov about 5 years
    Surprisingly, 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
    illusionist about 4 years
    its 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.