Skip to main content

Help with database design and associations

Databases • Asked by Alvaro Lobato

I have taken a look at Polymorphic associations and STI but I don't really know if they apply to my specific use case. In my app I have the following relevant classes:

class Restaurant < ApplicationRecord
 belongs_to :owner, class_name: "User"
 has_many :menus
 has_many :dishes
 has_many :categories
 has_many :bookings
 has_many :simple_bookings
 mount_uploader :photo, PhotoUploader
end

class Dish < ApplicationRecord
 has_many :menu_dishes
 has_many :menus, through: :menu_dishes
 belongs_to :category
 belongs_to :restaurant
 mount_uploader :photo, PhotoUploader
end

class Menu < ApplicationRecord  
 belongs_to :restaurant
 has_many :bookings
 has_many :categories
 has_many :menu_dishes
 has_many :dishes, through: :menu_disheshas_many :menu_dishes
end

class MenuDish < ApplicationRecord
 belongs_to :dish
 belongs_to :menu
end
So far the user flow is the following: A restaurant owner signs up and creates restaurant dishes. Then this owner creates a menu containing different MenuDishes. Afterwards, users can search for different menus and book them.

My problem is the following: I want to implement a feature where owners can add up to 3 MenuDishOptions (that belong to MenuDishes) so that a user can change each MenuDish with the available MenuDishOption for that MenuDish.

In other words, I want each MenuDish to contain many MenuDishOptions. Once a MenuDishOption is selected, the MenuDish passes to be a MenuDishOption and the selected MenuDishOption to a MenuDish.

Therefore the MenuDishOption class would look something like this:

class MenuDishOption < ApplicationRecord
 belongs_to :menu_dish
 has_one :dish
 belongs_to :menu, through: menu_dishes
end
The MenuDish class would be updated to:

class MenuDish < ApplicationRecord
 has_many :menu_dish_options
 belongs_to :menu
 belongs_to :dish
end
Please let me know if I need to share more info and thanks a million to anyone that takes the time to help this newbie.



Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.