Ruby on Rails Monday, August 1, 2011

> I didn't see it outside a model class so im having problems
> understanding and getting used to getter and setter outside a model
> class since its the same thing.

A model class in rails is just a means for accessing a database. Rails
automatically creates accessor methods for all the attributes/columns in
your database. But you can always add setters and getters to a class,
which is all a Model is, with attr_accessor or writing them out by hand.
But when you add any extra setters and getters to a model, they won't
correspond to any columns in the database--they are just programming, so
using the setter won't save anything to the database, and using the
getter won't retrieve anything from the database.

The methods in a model class are not 'actions' because rails doesn't map
urls to them. 'Actions' are the methods inside a controller class. And
rails maps urls to the actions, which is just a fancy way of saying that
when your rails app receives a request from a browser, rails executes
one of your actions. Which action rails executes when your app receives
a browser request is determined by the url of the request and which
action you tell rails to execute in response to that url in your
routes.rb file.

But both models and controllers are just ruby classes, so attr_accessor
can be used in both, and the rules about self are the same.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment