How to add a virtual attribute to a model in Ruby on Rails?

Ruby actually lets you create virtual attributes this way, which keeps you from having to manually create getter and setter methods:

attr_reader   :palindrome #getter
attr_writer   :palindrome #setter
attr_accessor :palindrome #both

You can also pass multiple arguments too:

attr_accessor :palindrome, :foo, :bar

The documentation for it isn't the greatest.


In your model, you can write attribute accessors (reader/writer) for your virtual attribute palindrome attribute this way:

# attr_reader
def palindrome
  self[:palindrome]
end

# attr_writer
def palindrome=(val)
  self[:palindrome] = val
end

# virtual attribute
def palindrome
  #return true/false
end

And, as you are using Rails 4, you have to whitelist palindrome attribute like any other model attribute in your strong param definition inside your controller in order to able to mass assign the value of palindrome. Something like this:

# your_controller.rb
private

def your_model_params
  params.require(:message).permit(:palindrome)
end

Take a look at this RailsCast on Virtual Attributes. Although, it's a bit old, but would be useful for concepts.

Note:

A virtual attribute will not show up in the param list automatically. But, you should be able to access it via Rails console like this: Message.new.palindrome. Also, you can expose this virtual attribute in your JSON API, for example if you are using Active Model Serializer, you can have: attribute palindrome in your MessageSerializer and then palindrome will be exposed to the JSON API.


Since Rails 5 you can also set virtual attributes like this:

attribute :palindrome, :boolean

It automatically casts the attribute to the specified type, which can be useful when the value comes from forms. This GoRails video shows some really good examples of both using the attr_accessor and the attribute approach. The documentation also includes some examples.