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

后端 未结 3 2023
小蘑菇
小蘑菇 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 05:01

    Solved this using a combination of stuff from Robin's link and some other SO answers

    class Album < ActiveRecord::Base
      set_table_name "album"
    
      class << self
        def instance_method_already_implemented?(method_name)
          return true if method_name =~ /^attributes/
          super
        end
      end
    
      belongs_to :artist
      has_many :tracks, :through => :album_tracks
    end
    

    Did the trick. I used a big sweeping change to return true without throwing an error for all methods starting with attributes, and I don't think its caused any problems elsewhere.

提交回复
热议问题