I'm working on a RubyonRails/ActiveAdmin application. My RoR version is 4.2.5 and AA version is 1.0.0. I have a model Message
as follows.
class Message < ActiveRecord::Base
belongs_to :user
validates :user, :content, presence: true
def palindrome
# return true/false
end
end
As you see, I want to have a read-only attribute palindrome
which only depends on the content
of message. I want this attribute to be treated exactly like a normal attribute. By normal, I mean when I retrieve messages via rails console
or request json
format of messages, I want to see a palindrome
attribute in the list. I would also like to have a filter for message by this attribute.
I'm not sure how could I achieve this.
Ruby actually lets you create virtual attributes this way, which keeps you from having to manually create getter and setter methods:
You can also pass multiple arguments too:
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: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 ofpalindrome
. Something like this: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 yourMessageSerializer
and thenpalindrome
will be exposed to the JSON API.