Rails model concept - sql

I am building an accommodation booking website on rails and I am confused about one aspect of the models. So normally the way I know of building it is by having a Room model that has many Reservations and the Reservation model belongs to the Room model. Problem is that this time I want the visitor to be able to book multiple rooms on one reservation. Is the Room has many Reservations association correct for that usage? Can anyone help me find a way of building that, do I have to use a single form that makes multiple records? I am sorry for my ignorance, it's the first time a concept like this fall into my hands.
Thank you all very much

You essentially need to have a many-to-many relationship defined and rails has a couple options to do so.
One option is the has_and_belongs_to_many relationship, which you can read up on here. However, my preferred option is the has_many, through: [model] approach as such:
class Room
has_many :room_reservations
has_many :reservations, through: :room_reservations
end
class Reservation
has_many :room_reservations
has_many :rooms, through: :room_reservations
end
class RoomReservation
belongs_to :room
belongs_to :reservation
end
essentially you have an intermediate table to create the many-to-many join.

Related

Storing Movie Sequels and Prequels

Given that I have a model that stores movies, what would be the best way to specify relationships such as movie sequels and prequels?
I'd like to access these relationships using simple accessors such as movie.sequels (which would return an ordered list of movies that are sequels to movie) and move.prequels.
I've considered using a has_many :through relationship with a secondary model but how would I maintain movie sequence?
Or could there be a better methodology entirely?
An easy way could be using an ActiveRecord plugin like acts_as_list which allows you to define ordering between different items. You'd need to add a field to use as a scope, such as saga, so the order is defined within movies which belong to the same saga.
https://github.com/swanandp/acts_as_list
The plugin provides handy methods lower_items and higher_items which would work like the sequels and prequels methods you are looking for,

Efficient Breadcrumbs On Rails Multi Model Tree?

I am new to rails and unclear about how to efficiently display breadcrumbs on a multi model tree hierarchy. My app tree is 7 models/levels deep but I will illustrate with 3.
class One
has_many :twos
class Two
belongs_to :one
has_many :threes
class Three
belongs_to :two
For example, say I want breadcrumbs on a show three view (linking to the parent instances of two and one). I am currently using AR associations in the controller:
add breadcrumb #three.two
add breadcrumb #three.two.one
This works fine except two additional SQL queries are run (to retrieve the two and one ancestors).
How can I have breadcrumbs more efficiently?
[Unfortunately, the Ancestry gem only seems to work with one model but is the idea of serialising the breadcrumb into a column worth pursuing? This is such a basic requirement that I suspect I am over complicating this out of ignorance.]

Is has_many => :through a violation of the Law of Demeter?

If it is, what's its defence? And if it isn't, why isn't it?
I think it is declaring new neighbor, akin to delegation. OTOH, if it were possible, it might be getting a bit bad to do multiple :through levels... (Do I remember correctly that they made this possible lately?)

Rails Controller: Is it good practice to define controller for associations?

Suppose i have mapping table map_user_roles. I have defined models Role and User. Both are associated to each other by relationship has_and_belongs_to_many. Of course it does not make sense to define model for mapping table in rails.
I have defined users_controller and roles_controller for crud operations on user and role respectively.
For association of user-role, what should i do? Should i define separate controller like user_roles_controller or should i make modifications in Role and User controller(if so how to do so) ?
Please suggest, what is good practice. Examples and good links would be great help
Thanks for devoting time.
I don't see what a separate controller for the association would offer that couldn't be achieved with your existing UsersController and RolesController. Also, note that sometimes is does make sense to define a model for the mapping table, that's what the has_many :through association is for. You should use it if you need to store extra attributes against the join model.

Rails routing with roles, how do I set it up?

This is more of a conceptual question...
I'm starting on an app that will have a few roles (e.g. employee, manager, store_manager). Each role will view/edit/destroy in different ways from each other. When a manager is editing an employee the view will be very different than when a store_manager is editing.
My thinking was to namespace many of the models with the role. Like:
namespace :store_manger do
resources :users
resources :widgets
end
namespace :manager do
resources :users
resources :widgets
end
This seems to be a much cleaner method than creating all the if can? :update, #article all over the controllers and views.
So, is my thinking on track?
Are there any pitfalls to using this method that I need to be aware of?
Or, is there a better way to organize this?
You're correct, grouping different roles into the same view/controllers doesn't make much sense in this case since the views are all different. Even if you need to share views later on, you can just render the same layout.