Accessing model properties in Rails

model, ruby, ruby-on-rails

Solution

I would hesitate to override the attributes, and instead add to the model like this:

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

However, you can access the fields directly like this:

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

Problem

So basically I have a controller. something like this ``` def show @user = User.find[:params[id]] #code to show in a view end ``` User has properties such as name, address, gender etc. How can I access these properties in the model? Can I overload the model accesser for name for example and replace it with my own value or concatenate something to it. Like in the show.html.erb view for this method I might want to concatenate the user's name with 'Mr.' or 'Mrs.' depending upon the gender? How is it possible?

Original source