views:

368

answers:

1

When do you use attr_reader/writer/accessor in Rails models?

+2  A: 

Never, unless you have specific need for it. Automatic database-backed accessors are created for you, so you don't need to worry.

Any attr_accessors you do create will change the relevant @attr on the rails object, but this will be lost when the object is destroyed, unless you stick it back in the database. Sometimes you do want this behavior, but it's unusual in a rails app.

Now in ruby, it's a different story, and you end up using these very frequently. But I'd be surprised if you need them in rails---especially initially.

Peter
perfect - thanks.(I'd plus vote you, but I just got on here and I seem to need 15pts first... That mechanic seems unfriendly to new users.)
Ethan