Extend model in plugin with “has_many” using a module

后端 未结 3 851
挽巷
挽巷 2021-02-10 08:28

I have a some code in an engine style plugin which includes some models. In my app I want to extend one of these models. I have managed to add both instance and class methods to

3条回答
  •  长情又很酷
    2021-02-10 09:19

    In Rails 3, this sounds like a good use case for ActiveSupport::Concern:

    module Qwerty::Core::Extensions::User
    
      extend ActiveSupport::Concern
    
      included do
        has_many  :hits, :uniq => true
        before_validation_on_create :generate_code 
      end
    end
    
    class User
      include Querty::Core::Extensions::User
      # ...
    end
    

    Here are the ActiveSupport::Concern docs and the most helpful blog article on it I've found.

提交回复
热议问题