Search code examples
ruby-on-railsrubymodelsspree

Modifying Spree models to add more relations


I created new models in my app which is using spree. I have a new model called lets say Something and I want to spree Product to have an one to many relationship with Something. So I added a folder in apps directory called spree and model called product.rb and the code inside it is

module Spree
  class Product < Spree::Base
    extend FriendlyId

    has_many :somethings
  end
end

If I run my console, my products have access to something but all other default spree product methods are not. I think I overriden them. What is the mistake I am doing? I wanna open the spree products class and add my new associations.


Solution

  • You gotta monkeypatch that thing!

    #{Rails.root}/lib/extensions/spree/product.rb

    Spree::Product.class_eval do |variable|
      has_many :somethings , class_name: Something ,:foreign_key => "something_id" 
    end
    

    /#{Rails.root}/config/application.rb

    config.autoload_paths += %W(#{config.root}/lib/extensions)