usage of attr_accessor in Rails

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.

Leave a Comment