views:

17

answers:

2

I have a 20 field database, and would like to set all the variables to be able to be accessed.

Is there a way to set attr_accessor to all of the variables, without listing each one i.e.

attr_accesor :a, :b, ... etc

A: 

I'm not sure if I understood your question (?), but if your model relates directly to the database, those properties are already accessible.

Chubas
so then is attr_accessor really only used for instance variables that are not directly related to the database?
Tian
A: 

attr_accessor is for adding get/set methods on a plain ruby object. With an ActiveRecord model, these are created automatically based on the columns in your schema.

Normally all ActiveRecord attributes are "accessible" which means you can mass-assign values to all of them from the params hash: Model.update_attributes(params[:model])

You might be thinking of attr_accessible which makes only certain columns accessible this way, and makes the rest "protected", so they can only be assigned directly through their setter method.

The opposite is attr_protected which leaves all columns accessible except the ones you specify.

Andrew Vit