I have four interrelated models: restaurants, menus, sections, and dishes. The dishes have_and_belong_to_many sections and ultimately are at the very bottom of the association chain. In my app I often need to reference the restaurant of the dish multiple times.
I know to accomplish this I'd need create a cascade of HMT associations. My question is, is it ideal to setup a belongs_to relationship between a restaurant and dish to avoid the multiple queries or leave as is? As of now this just feels dirty (may just be me though).
class Restaurant < ApplicationRecord
has_many :menus, dependent: :destroy
has_many :dishes, through: :menus
end
class Menu < ApplicationRecord
has_many :sections, dependent: :destroy
has_many :dishes, through: :sections
belongs_to :restaurant
end
class Section < ApplicationRecord
belongs_to :menu
has_and_belongs_to_many :dishes
end
class Dish < ApplicationRecord
has_and_belongs_to_many :sections
end
The denormalization you propose here seems reasonable to me given your use case. As always, the performance gains of denormalization must be weighed against the complexity it introduces, especially the need to write code to ensure that a the restaurant a dish belongs to is always the same restaurant its section's menu belongs to.
I guess my approach would be: Retain the normalized data model until you have an actual performance problem that needs to be addressed. Then you can consider denormalizing to address it.
While you are still have the cascade of HMTs, consider writing delegators to make the code as clean as if you had a direct restaurant-dish association. I'm thinking:
class Dish < ApplicationRecord
delegate :restaurant, to: :section
end
class Section < ApplicationRecord
delegate :restaurant, to: :menu
end
Then you can do:
dish.restaurant
Related
I'm uncertain of the best approach to model conversation relationships.
My application has Users, Conversations, UserConversations, Venues, & VenueConversations.
Users can have many Conversations & Conversations can have many Users. Thus, the bridging table (UserConversations).
Venues can also have Conversations and these Conversations can also have many Users. Thus, the bridging table (VenueConversations)
I've tried it with polymorphic associations but then Messages in the Conversations became difficult to manage. Also, a while back I asked a question on Stack and someone said I should steer clear of polymorphic associations at all costs.
I hope to do this properly so that querying doesn't become a nightmare in the future.
I'd also like to state I'd like to have one Conversation model because one day they'll share a lot of behavior.
User.rb
class User < ApplicationRecord
has_many :user_conversations
has_many :conversations, through: :user_conversations
has_many :venue_conversations
has_many :conversations, through: :venue_conversations
end
UserConversation.rb
class UserConversation < ApplicationRecord
belongs_to :user
belongs_to :conversation
end
Venue.rb
class Venue < ApplicationRecord
has_many :venue_conversations
has_many :conversations, through: :venue_conversations
end
VenueConversations.rb
class VenueConversations < ApplicationRecord
belongs_to :user
belongs_to :venue
belongs_to :conversation
end
The problem I'm having now is that when I do User.first.conversations I only find one of the types of conversations a User could have(Private vs Venue). It reflects the last definition I gave to has_many :conversations in the User.rb model. Perhaps a scope is in order? How would I make a distinction between private & venue conversations as well?
Please advise me on how I could resolve this or take a better approach
I'm looking to create a simple relationship model with the following:
I have 2 main items. Product / WishList
The relationship between them is pretty straight forward a WishList has_many :products and a Product belongs_to_many :wishlists
I understand that there isn't a belongs_to_many association and as simple as this may be I can't seem to wrap my head around the right relationship model.
If someone could suggest a nice way to achieve this it would be much appreciated.
Thanks.
You have a choice between a has_many :through and a has_and_belong_to_many relation.
Pick the first one if you need to attach other attributes to the model that will be the bridge (for instance a position, if you want to order your wishlist), or the second one otherwise (more about that here)
So it could look like so
class Product < ActiveRecord::Base
has_many :items
has_many :wishlists, through: :items
end
class Item < ActiveRecord::Base
belongs_to :product
belongs_to :wishlist
end
class Wishlist < ActiveRecord::Base
has_many :items
has_many :products, through: :items
end
I need some advice. I am pretty new in modeling database so I was wondering what would be the most convenient way to model database which would make my life easier. I have one Car item which can have multiple descriptive subcategories, like TireCategory, GlassCategory, EngineCategory...
I was thinking about doing something like this:
class Car < ActiveRecord::Base
belongs_to :tire_category
belongs_to :glass_category
belongs_to :engine_category
#Car would have three additional columns: tire_category_id, glass_category_id, engine_category_id
end
class TireCategory < ActiveRecord::Base
has_many :cars
end
class GlassCategory < ActiveRecord::Base
has_many :cars
end
class EngineCategory < ActiveRecord::Base
has_many :cars
end
Is this recommended way to do it or some other relation would be more suitable? Thank you!
You can store cars (with uniq data like model name and sku) in one table, properties (like engines, glasses) in another, and one more table for relation between cars and properties.
class Car < ActiveRecord::Base
has_many :car_properties, dependent: :destroy
has_many :properties, through: :car_properties
end
class Property < ActiveRecord::Base
has_many :car_properties, dependent: :destroy
has_many :cars, through: :car_properties
end
class CarProperty < ActiveRecord::Base
belongs_to :car
belongs_to :property
end
If you have a lot of properties and want to logically separate them then create separate models for each property. Like Engine, Tire, Glass. Each model will contain properties variants. Then create one table for each property to connect it to the car. Like CarEngine, CarGlass. It will contain car_id and property_id (engine_id, glass_id).
I have a User model:
class User < ActiveRecord::Base
has_many :projects, dependent: :destroy
end
and a Project model:
class Project < ActiveRecord::Base
belongs_to :user
end
What should I do if I want a User to be able to fund Projects, and a Project can be funded by many Users?
This would mean I get a Many-to-many relationship, and I would need an additional intermediate table. Call it user_projects:
class UserProject < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
But how do I cope with the previous relationship I had between the models before I implemented the third one?
How do I know which project belongs to which user if I have the intermediate table?
Would I modify the tables the following way?
class User < ActiveRecord::Base
has_many :projects, through: :user_project, dependent: :destroy
has_many :user_projects
end
class Project < ActiveRecord::Base
has_many :user_projects
has_many :users, through: user_project
end
Whether or not you need an intermediate table depends on if there is any associated data you need to store with the user/project pair -- such as a dollar amount or date info or role, etc.
If you don't need to store anything else, then just use a HABTM relationship. Otherwise, your final solution would be the way to go.
I personally don't like the choice of 'user_project' as it's too close to the HABTM's 'users_projects'. Perhaps something like ProjectMember or ProjectFunder or Funding would be better, but it kind of depends on what extra data you need to store.
So I'm Rails n00b and I want to create a "favorites" relationship such that a User can have many favorite Item. I'm not entirely sure how to do this, this is how I'm going to try but I'm not sure if this is a good practice at all:
class User < ActiveRecord::Base
has_many :favorites
//other code
end
class Favorite < ActiveRecord::Base
belong_to :user
has_one :item
end
class Item < ActiveRecord::Base
belongs_to :item
end
Is this a good way to do it? Should I be using has_and_belongs_to_many ?
I'm specially concerned in the following scenario: Say a user has 100 favorite items.
When I do a User.find(id) will I also be retrieving the 100 favorites and the 100 Items?
In case it's important: ruby version 1.9.3, rails version 3.2.11
Can you try has_many => :through?
class User < ActiveRecord::Base
has_many :favorites
has_many :items, :through => :favorites
//other code
end
In your case has_many :through is definitely the way to go. I would recommend reading: http://guides.rubyonrails.org/association_basics.html
Of particular interest with regard to your question:
2.8 Choosing Between has_many :through and has_and_belongs_to_many
Rails offers two different ways to declare a many-to-many relationship between models. The simpler way is to use has_and_belongs_to_many, which allows you to make the association directly:
class Assembly < ActiveRecord::Base
has_and_belongs_to_many :parts
end
class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end
The second way to declare a many-to-many relationship is to use has_many :through. This makes the association indirectly, through a join model:
class Assembly < ActiveRecord::Base
has_many :manifests
has_many :parts, :through => :manifests
end
class Manifest < ActiveRecord::Base
belongs_to :assembly
belongs_to :part
end
class Part < ActiveRecord::Base
has_many :manifests
has_many :assemblies, :through => :manifests
end
The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity. If you don’t need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you’ll need to remember to create the joining table in the database).
You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.
It is better than using has_and_belongs_to_many.
When I do a User.find(id) will I also be retrieving the 100 favorites
and the 100 Items?
No. You'll just get the user object.
Update:
Calling User.include(:favourites, :items).find(id) will get you joined tables in case you want to make many calls to items table from user object.