I'm developing online shop for experience. I stuck on modeling products relationship.
This is my example. (--> = "has_many"):
Country --> Labels(vendors) --> Collections --> Products
Models
class Country < ActiveRecord::Base
has_many :labels
has_many :collections, through: :labels
end
class Label < ActiveRecord::Base
belongs_to :country
has_many :collections
end
class Collection < ActiveRecord::Base
belongs_to :label
has_one :country, through: :label, source: :country
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :collection
has_one :label, through: :collection, source: :label
has_one :country, through: :label, source: :country
end
I should use every model like category or subcategory. Example
Brazil/SuperLabel/NightCollection/SuperProduct
But every product should have country, label and name of collection in description.Example
price: 100.0$
country: Brazil
collection: NightCollection
label: SuperLabel
I use this approach Product.all.includes(:country, :label, :collection)
, but i think this is wrong solution and not rails way.
Questions:
1) What other solutions are there? How i can fetch country of product, label of product and collection in product model? My solution is bad or not? maybe another approach or maybe another relationship between models
2) Controllers.Should i use separate controllers for country, labels etc and create products with nested attributes (using created early variants) or use nested resources? If i use nested resources, what about this rule Resources should never be nested more than 1 level deep.
What the best solution?
1) for example
resources :countries do
resources :labels, only: [:new, :create]
end
resources :labels, only: [:index, :show, :edit, :update, :destroy]
resources :labels do
resources :collections, only: [:new, :create]
end
resources :collections, only: [:index, :show, :edit, :update, :destroy]
resources :collections do
resources :products, only: [:new, :create]
end
resources :products, only: [:index, :show, :edit, :update, :destroy]
I think it's a bit massive approach second solution:
resources :countries
resources :labels
resources :collections
resources :products
When i will create product i will add country, label etc , that i created early in country controller, label controller etc separately. I want simple admin panel, without difficult links.
what is best approach and what pitfalls in every of this approaches? Maybe all approaches are not good and exist another solutions.
Product.all.includes(:country, :label, :collection)
simply fetches all products. You would also fetch by typing Product.all.includes(:collection => [:label => :country])
(you do not need these has_one
associations then) and it gives exactly the same result as the previous one. By result I mean not all products, but all remaining associations eager load with the same amount of queries.
Regarding model relationships, again, it depends what you need. The declaration of your relations is not as important as your database tables and indexes. Any has_one
or has_many
is just to tell ActiveRecord
, how to associate model A
with model B
and fetch data eventually. What's important here, you're already using includes
for eager loading.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.