How can I permanently ignore a database column in my ActiveRecord::Base class?

后端 未结 3 2058
小蘑菇
小蘑菇 2021-01-18 04:25

I have a legacy database which I\'m trying to model using Rails. One of the tables has a column named attributes, which is a name reserved by Rails I think.

3条回答
  •  天命终不由人
    2021-01-18 04:59

    (This is an old question but still figures in Google so I'll add a belated answer)

    I've found the best way to restrict data loaded by an ActiveRecord model is to create a database view that only contains the columns you wish to load. You can point your ActiveRecord model at the constrained view using ActiveRecord's table_name method. Native SQL tools can still manipulate the underlying table, but ActiveRecord will only see (and consequently load) the columns explicitly included in the view.

提交回复
热议问题