Reputation: 2388
I want to add Service
category, same like Spree::Product
, for that I have to define some associations, as below
class Service < ActiveRecord::Base
has_many :images, -> { order(:position) }, as: :viewable, class_name: "Spree::Image", dependent: :destroy
has_many :taxons, class_name: "Spree::Taxon", dependent: :destroy
validates :name, presence: true,
length: { minimum: 5 }
end
Now, first, is this the right method to define such category or should i use some other convention to define Service
, and for :taxons
association, should I define migration to add service_id
column in spree_taxons
table?
Upvotes: 1
Views: 661
Reputation: 1001
There is a matter of design there, Spree uses a model to join Taxons and Products, you should create it and name it services_taxon, the migration should look something like this:
class CreateServiceTaxon < ActiveRecord::Migration
def change
create_table :service_taxon do |t|
t.integer :service_id
t.integer :taxon_id
end
end
end
And on the Service model you should add:
class ServiceTaxon < ActiveRecord::Base
belongs_to :service, :class_name => 'Service', :foreign_key => 'service_id'
belongs_to :taxon, :class_name => 'Spree::Taxon', :foreign_key => 'taxon_id'
end
Another thing i should point out is that if you need some functionality that is already created by the spree team on the product model, you should really consider using theirs, or at least try to extend the product model.
Upvotes: 1
Reputation: 68681
You would need a new join model such as ServiceTaxons
instead of adding a service_id
to Spree::Taxon
. If you look at how Products
are linked to Taxons
it's through the spree_product_taxons
table.
The more important part is whether or not you need a new Service
class. You would be better off having your services as just products. Products are deeply ingrained into the Spree system that you are creating a lot of work for yourself trying to implement another model that exists side-by-side with it.
Upvotes: 0