Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: accessing field value from model method

Just started learning Rails (3). I am tearing my hair out trying to find how to do something presumably utterly trivial: access the value of a model instance's field, from inside a method on that model.

In my case:

def formal_name
  @title + " " + @forename + " " + @surname
end

All three @properties (which are all fields on the table in the database) return nil. They shouldn't.

Incredibly, how to access fields isn't discussed at http://guides.rails.info/, and google turns up nothing.

BTW, I'm coming from Django where this stuff is obvious.

like image 443
jameshfisher Avatar asked Jun 24 '10 14:06

jameshfisher


1 Answers

The @ syntax is used for instance variables that (for example) get populated in controllers and then used in views. Not what you're doing here.

You actually just need

def formal_name
  title + " " + forename + " " + surname
end
like image 85
Jacob Mattison Avatar answered Nov 02 '22 03:11

Jacob Mattison