Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord object attribute name same as method name

I'm trying to call a method on an ActiveRecord object where there is an attribute of the same name. This results in a SystemStackError: stack level too deep error.

How can I access the object attribute from within the method in order to modify it?

def first_name

 return self.first_name.upper

end
like image 733
hiei Avatar asked Apr 02 '12 11:04

hiei


People also ask

What are rails attributes?

Defines an attribute with a type on this model. It will override the type of existing attributes if needed. This allows control over how values are converted to and from SQL when assigned to a model. It also changes the behavior of values passed to ActiveRecord::Base.

What does ActiveRecord base do?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is ActiveRecord?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

What is Ruby ActiveRecord?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


1 Answers

If this is an Active Record model you can use read_attribute method

def first_name
  read_attribute(:first_name).upper
end
like image 193
mikdiet Avatar answered Oct 13 '22 00:10

mikdiet