Naming Boolean columns in Rails
You should call it trained
. Define it in your schema with a type of :boolean
. You can refer to it as trained?
and everything will magically work.
So says http://www.ruby-forum.com/topic/60847
Naming conventions for boolean attributes
I think of it like this:
- A boolean attribute should answer a simple yes/no question.
- Asking "is?" is a simple way to ask a yes/no question.
- So a boolean attribute name should complete the sentence
is <attribute_name>
- Then just remove the
is
because Ruby prefers the?
suffix for "is"-style method names (see below for more), leaving you with justemail_hidden
So, for your specific case, name your attribute like this:
- Start with the question "email is hidden"?. Which can be represented in code as:
email_is_hidden
. - Remove the
is
and you're left withemail_hidden
- Ruby on Rails defines a "predicate" version (one that ends in a
?
) of your boolean attribute and returnstrue
/false
as expected. So, while your attribute is namedemail_hidden
in the database you should reference it asemail_hidden?
in your code for clarity and as is the Ruby on Rails convention.
rails boolean fields: `is_foo` or just `foo`?
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.
Can't come up with a Rails-ish boolean column name
A good name is one of the hardest things to do in coding and I too spend silly time mulling over good naming.
One of my judging criterion is see how the name is being used.
In database point of view i.e. reading from the model source code, only_owner_can_create
is a good explanatory name.
In controller point of view, you may want to write if group.allows_members_to_create?
or unless group.only_owner_can_create
Having the context where this field will be used will help you greatly to determine the name.
Also you CAN have the best of both worlds - just use alias_methods to add more methods!
Rails: get all boolean columns from model and map their name and value
It will get all the column names filtered by the desired type,
Template.columns.select{ |c| c.type == :boolean }.map(&:name)
ruby / rails boolean method naming conventions
I think that depends on the action you want to perform and the action you are checking for. I would aim for readability and least amount of confusion:
self.run
self.can_run? # can this object run?
self.running_allowed? # is running allowed here or by this user?
self.redeem!
self.redeemable? # is this object currently redeemable?
self.copy_to_clipboard
self.copy_to_dashboard
self.copyable? or self.can_be_copied? #can this object be copied
self.copy_allowed?(:clipboard) # is copying to the clipboard allowed by me?
How to define boolean field for a rails migration
Yes, you can use :boolean
for this, and yes it will also save database space.
Rails: list column name if true
Here's the general idea of looping through an objects attributes with the attribute name and value. Are you having to filter out only boolean fields? Or are all the fields boolean?
@preference.attributes.each do |attr_name, attr_value|
"#{attr_name} is #{attr_value}" if attr_value == true
end
Ruby: Boolean attribute naming convention and use
Edit: three-years later; the times, they are a-changin'…
Julik's answer is the simplest and best way to tackle the problem these days:
class Foo
attr_accessor :dead
alias_method :dead?, :dead # will pick up the reader method
end
My answer to the original question follows, for posterity…
The short version:
You can't use a question mark in the name of an instance variable.
The longer version:
Take, for example, attr_accessor :foo
— it's simply conceptually a bit of syntactic sugar for the following:
def foo
@foo
end
def foo=(newfoo)
@foo = newfoo
end
Furthermore, the question-mark suffix is mostly just a convention to indicate that the return value of a method is a boolean.
The best approximation I can make of what you're going for here…
class MyClass
def initialize
@awesome = true
end
def awesome?
@awesome
end
end
In this case, there may be a case to be made for using attr_accessor
— after all, it may be explicit that you're working directly with a boolean attribute. Generally, I save the question-mark suffix for when I am implementing a method whose boolean return value is based on slightly more complex conditions than just the value of an attribute.
Cheers!
Edit, two years later, after a recent comment:
- Ruby enforces certain naming conventions.
Symbols in Ruby can't have question marks. Thus invocations ofEdit: not correct, just use the quoted syntax for a symbol, e.g.,:my_boolean_attribute?
both will fail with aNameError
.:"my_attribute?"
- Symbols are immutable, attempting to assign to one will throw a
SyntaxError
.
Related Topics
Is There a Less Intrusive Alternative to Rspec's 'Should_Receive'
How to Convert a String to a Class Method
One Liner in Ruby for Displaying a Prompt, Getting Input, and Assigning to a Variable
How to Find If a String Starts with Another String in Ruby
How to Get Elapsed Time in Milliseconds in Ruby
When to Use a Lambda in Ruby on Rails
Ruby on Rails: Devise, Want to Add Invite Code
Rspec: How to Test If a Method Was Called
Cannot Execute "Rails Console" Due to an Error with Readline
How to Modify an Array While I am Iterating Over It in Ruby
How to Create a Full Audit Log in Rails for Every Table
How to Loop Over a Hash of Hashes
I Want to Override Authenticate_User and Current_User Method of Devise Gem
How to Check If a Class Is Defined
How to Remove a Substring After a Certain Character in a String Using Ruby