114

I'm trying to overwrite a getter method for an ActiveRecord model. I have an attribute called name in the model Category, and I'd like to be able to do something like this:

def name
  name_trans || name
end

If name_trans attribute is not nil, then return it, else return name attribute. How would I do this?

This should then be called normally like this:

@category.name
user229044
  • 232,980
  • 40
  • 330
  • 338
oyvindhauge
  • 3,496
  • 2
  • 29
  • 45

7 Answers7

164

The Rails Style Guide recommends using self[:attr] over read_attribute(:attr).

You can use it like this:

def name
  name_trans || self[:name]
end
user229044
  • 232,980
  • 40
  • 330
  • 338
Wonsup Lee
  • 1,780
  • 1
  • 11
  • 6
  • Why is it better? Don't just link to a other sites, include the relevant bits here. Links can become invalid – JamesT Feb 06 '15 at 09:51
  • 2
    For a bit more context see here: https://github.com/bbatsov/rails-style-guide/issues/155 – mimsugara Apr 20 '16 at 09:25
101

Update: The preferred method according to the Rails Style Guide is to use self[:name] instead of read_attribute and write_attribute. I would encourage you to skip my answer and instead prefer this one.


You can do it exactly like that, except that you need to use read_attribute to actually fetch the value of the name attribute and avoid the recursive call to the name method:

def name 
  name_trans || read_attribute(:name)
end
Community
  • 1
  • 1
user229044
  • 232,980
  • 40
  • 330
  • 338
28

I would like to add another option for overwriting getter method, which is simply :super.

def name
  name_trans || super
end

this works not just on attributes getter method, but also associations getter methods, too。

lei liu
  • 2,735
  • 1
  • 16
  • 18
  • 2
    This is what the Rails guides currently recommend, for getters AND setters: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#class-ActiveRecord::Base-label-Overwriting+default+accessors – sandre89 Jun 30 '18 at 11:20
5

Overriding the getter and using read_attribute does not work for associations, but you can use alias_method_chain instead.

def name_with_override
  name_trans || name_without_override
end

alias_method_chain :name, :override
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
2

If you using store attributes like this

store :settings, accessors: [:volume_adjustment] 

or using gems like hstore_accessor gem link

So you ended up using store method on model, then to override those method you can't use self.read_attribute, you must use instead super like that:

def partner_percentage
  super.to_i || 10
end
Ajay Barot
  • 1,681
  • 1
  • 21
  • 37
onemanstartup
  • 355
  • 2
  • 10
1

If someone want update the value after name_trans in getter method, you could use self[:name]=.

def name
  self[:name] = name_trans || self[:name]
  # don't do this, it will cause endless loop
  # update(name: name_trans)
end
Fangxing
  • 5,716
  • 2
  • 49
  • 53
0

You can use Rails read_attribute method. Rails docs

Rajat Bansal
  • 885
  • 9
  • 7
  • while this link might answer the question, link only answers are not allowed on SO, please add some context to your answer from what's behind that link. Thanks! :) – AT82 Feb 17 '17 at 10:03