Access Model Properties in Rails

So basically I have a controller. something like that

def show @user = User.find[:params[id]] #code to show in a view end 

The user has properties such as name, address, gender, etc. How can I access these properties in the model? Can I overload the model to access the name, for example, and replace it with its own value or associate something with it. As in the show.html.erb view for this method, I could associate the username with 'Mr.' or "mrs" depending on gender? How is this possible?

+6
ruby ruby-on-rails model
source share
6 answers

I would not redefine attributes, but instead added to the model:

 def titled_name "#{title} #{name}" end 

However, you can access the fields as follows:

 def name "#{title} #{self[:name]}" end 
+9
source share

You can create virtual attributes in your model to represent these structures.

There is railscast in this question, but in the end you can do something similar in your model

 def full_name [first_name, last_name].join(' ') end def full_name=(name) split = name.split(' ', 2) self.first_name = split.first self.last_name = split.last end 

If you want to explicitly change the attribute value when reading or writing, you can use the read_attribute or write_attribute methods. (Although I believe that they may be outdated). A.

These works replace the access method of this attribute with your own. For example, a branch identifier field may be entered as xxxxxx or xx-xx-xx. This way you can change your branch_identifier = method to remove hyphens when data is stored in the database. This can be done like this:

 def branch_identifier=(value) write_attribute(:branch_identifier, value.gsub(/-/, '')) unless value.blank? end 
+5
source share

You can easily overload attributes as you suggest.

i.e. if the name is a field in the user database table, you can do:

 def name "#{title} #{read_attribute[:name]}" end 

The read_attribute function will return the database column value for the field.

Caveat . I'm not sure this is a good idea. If you want to use a method that displays model data in a modified way, I will be tempted not to overload the default methods and call them something else - this will avoid a certain level of obfuscation.

Documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html (in the section "Overwriting default accessories")

0
source share

If you get access to data stored directly in the database, you can do this when viewing:

 <%= @user.firstname %> <%= @user.gender %> 

and etc.

If you need to create custom views of the data, you need to either create helpers or extend the model (as described above).

0
source share

I usually use helper methods added to the model for things like this:

 def formatted_name "#{title} #{first_name} #{last_name}" end 

(Edit the previous post. I looked at my code and realized that helpers should only be for materials related to the presentation ().

(Edit again to remove the parameter on the left ... Geez, coffee is missing this morning.)

(Change again to replace $ with # ... Maybe I just need to delete this, right?)

0
source share

at http://api.rubyonrails.org/classes/ActionController/Base.html

do a search

 Overwriting default accessors 
-one
source share

All Articles