60

Possible Duplicate:
Naming Boolean columns in Rails

What is the rails convention regarding names of boolean fields?

For example, if I have a User model that needs a flag for its "activeness", should I call the db field is_active or active ?

Note: Rails automatically generates question-marked methods for accessing boolean fields: User.is_active? and User.active?.

Community
  • 1
  • 1
kikito
  • 51,734
  • 32
  • 149
  • 189

2 Answers2

67

The plain-adjective form is easily the norm in Ruby and Rails — even?, nil?, empty? and blank? for example. The only method of the form is_#{something}? that I can think of is Kernel#is_a? to determine class identity. So to stick with standard naming conventions, I would leave off the is_ on boolean methods like this.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 1
    thanks I appreciate your comment. However I believe `is_xxx` will be more appropiate for my application. I've run up to a boolean called `is_client`. Replacing it by `client` makes it look like an ActiveRecord association (eventhough I don't have a Client model on my design... for now). – kikito Jun 24 '10 at 21:34
  • 8
    I think what Chuck has eluded to here is the inclusion of the question mark. It would be `client?` (including the question mark) not `client` or `is_client`. The `is_` prefix is for languages that don't support adding a question mark to the name. To define it is simply: `def client? ... end` – PhilT Jul 03 '12 at 12:27
  • 6
    Also, Rails creates corresponding methods for boolean values. So if you have a field called client that is a boolean then Rails will creates a method on that model called `client?` that allows you to check if it's set. – PhilT Jul 03 '12 at 12:39
  • 6
    Besides it being the norm, one advantage of avoiding the `is_` prefix if you are using RSpec is that [predicate matchers](https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/predicate-matchers) will read much better. E.g., `expect(user).to be_active` – epidemian Mar 25 '14 at 14:55
  • to be fair `is_a?` is not `is_"something"` because `a` it's just the article, not the object.. hence why it's allowed :P – Andre Figueiredo Mar 21 '20 at 18:49
9

Of the 2 you should choose the one that sounds better to you: User.active? or User.is_active?

I'd personally opt for the former.

The question mark goody comes from Ruby, not Rails.

allesklar
  • 9,506
  • 6
  • 36
  • 53
  • Your gave me the solution to my problem - I ended up using what I thought was most correct (`is_client`). Chuck however answered the question - Rails (and Ruby) seem to favour the plain-adjective. I would share the answer between the two of you if I could. I can only give you a +1 and my thanks. Also, thanks for the pointers regarding the question mark - I didn't know it came from ruby itself. – kikito Jun 24 '10 at 21:38
  • 1
    I really hate "follow your heart" answers. Non-answers, really. – Arcolye Nov 22 '12 at 15:55
  • 1
    I choose the later than the former. User.active to me could be a named scope. For me it's just more clear that .active is a scope and .is_active is a boolean. Yeah you have the ? on the method, but: User.active, user.active?, user.is_active? -- the last one is just easier to contextually recognize, imho. – Nicholas C Mar 08 '13 at 23:32
  • 1
    One alternative is to split the baby. Name the field `is_active`, but add your own boolean methods for interrogation. I like having both a positive and a negative, so I'd define `active?` and `inactive?`. If I need to set the value, I use `is_managed=`, but I try to always use the `active?` or `inactive?` forms as appropriate in expressions. – tovodeverett May 24 '13 at 22:01
  • @Arcolye Programming is a kind of art, ie it requires a lot of creativity and it is your responsibility as a programmer to write nice looking code. So where there is no naming convention you just have to _follow your heart_. +1 – Arne L. Aug 07 '13 at 07:05