How can I overwrite a getter method in an ActiveRecord model?

Ruby on-RailsRubyRails ActiverecordGetter Setter

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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。

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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

Solution 7 - Ruby on-Rails

You can use Rails read_attribute method. Rails docs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionoyvindhaugeView Question on Stackoverflow
Solution 1 - Ruby on-RailsWonsup LeeView Answer on Stackoverflow
Solution 2 - Ruby on-Railsuser229044View Answer on Stackoverflow
Solution 3 - Ruby on-Railslei liuView Answer on Stackoverflow
Solution 4 - Ruby on-RailsPatrick OscityView Answer on Stackoverflow
Solution 5 - Ruby on-RailsonemanstartupView Answer on Stackoverflow
Solution 6 - Ruby on-RailsfangxingView Answer on Stackoverflow
Solution 7 - Ruby on-RailsRajat BansalView Answer on Stackoverflow