开发者

set Attr_accessor for all instance variables

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 on开发者_开发百科e i.e.

attr_accesor :a, :b, ... etc


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜