Rails: Overriding ActiveRecord association method

后端 未结 7 1360
忘了有多久
忘了有多久 2020-12-05 02:21

Is there a way to override one of the methods provided by an ActiveRecord association?

Say for example I have the following typical polymorphic has_many :through ass

相关标签:
7条回答
  • 2020-12-05 03:10

    You can use block with has_many to extend your association with methods. See comment "Use a block to extend your associations" here.
    Overriding existing methods also works, don't know whether it is a good idea however.

      has_many :tags, :through => :taggings, :order => :name do
        def << (value)
          "overriden" #your code here
          super value
        end     
      end
    
    0 讨论(0)
提交回复
热议问题