Alvaro Lobato

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Help with database design and associations


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.


Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.