Nested routes without inadvertent top level routes - ruby-on-rails

I have 3 resources that are structured in a hierarchy as follows:
house 1-* residents 1-* books
We know that its not great to have deeply nested routes, so we endeavour to have routes nested at most 1 resource deep. Defined something like:
resources :houses do
resources :residents
end
resources :residents do
resources :books
end
The problem is that we end up defining :residents as a resource that can be accessed without any nesting - as part of the definition of :books. Is there way to define :books as being nested in :residents, without inadvertently registering :residents as a top level route?

This can be accomplished by using scopes, in your case:
scope 'residents/:resident_id' do
resources :books
end
However, as I mentioned in the comment, this practice can end up confusing users who expect URLs to behave in a certain way (deleting the last bit takes them up a level).

Related

Handling Rails routes/models with belongs_to relationship

So this may be more of a convention question, but im writing a todo app to learn how to use rails as an API (Im somewhat intermediate with using rails normally) but this time im using it with React for the front end.
Im making a simple todo app, two models in particular being "Lists" and "ListItems". Lists has_many ListItems of course, and a ListItem belongs_to a List.
So naturally I have the routes set up like so:
resources :lists do
resources :list_items
end
Giving me routes similar to: /api/v1/lists/:list_id/list_items etc.., However I saw some people doing a similar app set it up like:
namespace :v1 do
resources :list_items
resources :lists
end
Which confuses me because how would you handle passing the actual "List" params to the route, when the route itself would not have a List_id param?
Or would this more be used for a join table somehow..but you would still have to populate the List_id regardless when creating a list_item for a specific list correct?
Is there a preferred way of doing this as far as routing goes? (And I suppose creating tables?) Since a has_many_through seems not really necessary in this case?
Unless there is more to the story, you are doing it the more conventional way. I suggest your can safely disregard that not-nested approach. The only enhancement I suggest is using shallow: true, like:
namespace :api do
namespace :v1 do
resources :lists do
resources :list_items, shallow: true
end
end
end
You can read more about shallow nesting in the guide.

Design nested resources and individual resources actions coexisted for Ruby on Rails

I have a user case in which nested resources and individual resources both make sense. How can I design path and code structure to reflect this? For example:
resources :magazines do
resources :ads
end
resources :ads
Each ad belongs to a magazine, so magazines/:magazine_id/ads absolutely make sense. However, in some cases, I want to list all ads only, so ads also make sense. Please note, magazines/:magazine_id/ads and ads have completely different view. The same thing applies to other actions. Does anyone have similar situation? And what is the better way to solve it and structure code in mvc?
Your routes should be fine, you can handle the difference in a controller - there will be difference in params: :magazine_id will be present or absent.
If you want to separate the controllers completely, you can use module: in your routes:
resources :magazines do
resources :ads, module: 'nested'
end
resources :ads
The first one would point to Nested::AdsController, the second to AdsController
Or the other way around
resources :magazines do
resources :ads
end
resources :ads, module: 'unnested'
First one points to AdsController, second to Unnested::AdsController.

How to properly avoid nesting routes more than 1 level deep in Rails

I'm having a hard time conceptualizing the best way to set up the resources for this project.
Here are my current routes:
resources :customers do
resources :jobs
end
resources :jobs
resources :rooms
resources :memos
resources :appliances
resources :accessories
end
I took my structure from this question: Rails 3 routing: Avoiding Deep Nesting
My question(s) are as follows:
Am I doing this right?
If yes, what does this accomplish that nesting more than 1 level deep does not? I'm assuming it's just absolute paths for things like jobs/rooms/id, instead of customers/jobs/rooms/id - but I don't know if this is correct.
Any further education will be appreciated. Thanks!
Yes, you are doing it right. You could either nest the resources or not, it is up to you.
Nesting one more level usually "forces" you to find each resource.
Consider these routes:
resources :customers do
resources :jobs do
resources :rooms
end
end
Then you would have relative paths like /customers/1/jobs/2/rooms/3/show.This implies that the Room #3 belongs to Job #2 which belong to Customer #1.
In other words, you would end up for the show action of the RoomsController with 3 instances: #customer, #job and #room, all set by a SQL query.
But you already defined that a room belongs to a job and that a job belong to a room. So do you really have to run 3 SQL queries? No, which is why you avoid deeply nested routes (over 2~3 levels).

Should routes be nested for associations?

I have a one-to-many association between User and Occupation (a user has_many :occupations). In the routes file, I did:
resources :users do
resources :occupations
end
to nest the occupations routes inside the users. Playing around with AJAX requests, I realized it's easier for me to not have the occupations route nested, like this:
resources :users
resources :occupations
My question is, do I lose (performance, functionality) in any way by not having the routes nested?
Update: Aside from losing the users/1/occupations routing. I know that I won't get that if I don't nest the routes.
I wouldn't worry about the performance (if anything, the nested routes might be slightly slower) and just design the routes that make the most sense for your application.

Rails RESTful delete in nested resources

Okay, so here's an example scenario. There is a student resource resources :students, and students has and belongs to many collections: resources :clubs, resources :majors, etc.
So we can set up our routes easily enough...
resources :clubs do
resources :students
end
resources :majors do
resources :students
end
resources :students do
resources :clubs
resources :majors
end
which generates us a bunch of standard RESTful routes
/clubs
/clubs/:id
/clubs/:club_id/students
/clubs/:club_id/students/:id
/majors
/majors/:id
/majors/:major_id/students
/majors/:major_id/students/:id
/students
/students/:id
/students/:student_id/clubs
/students/:student_id/clubs/:id
/students/:student_id/majors
/students/:student_id/majors/:id
So here's my question. With REST semantics, how would one delete a major for a student?
Browsing students under a major /majors/:major_id/students/:id would show that student in the specific major's 'collection'. But the route for deleting an :id, points to StudentsController#destroy, which would delete the student completely. Whoops! So maybe we go the other way, and perform DELETE on the resource at /students/:student_id/majors/:id and well now UnderwaterBasketweaving is now no longer offered at this school...Whoops!
Now granted, we could set the destroy method of ClubsController, MajorsController, or StudentsController to look for club_id, or major_id, or student_id, but lets say we also down the road want to add Fraternities and GraduatingClasses, etc. Each class would start to be comprised of huge switch conditionals looking to see what param was present... and then find the collection of either the top resource and remove the bottom resource, or vise versa. The Models themselves should decide whether they delete themselves if they have no more association records... 'Destroy' on that resource has become really a misnomer...
Is there an easier way to do this? Even popular restful rails plugins like make_resourceful or resource_controller would blow away UnderwaterBasketweaving when removing it from Joe's Majors or completely delete JohnDoe when removing him from the major UnderwaterBasketweaving. It would seem there's a potential for looking at the association to understand the desired effects of the semantics and what 'destroy' should do.
Then again, am I looking at this all wrong? Is it not UnderwaterBasketweaving -> Joe but UnderwaterBasketweaving+Joe as a single resource and what we're deleting is truly not Joe, nor UnderwaterBasketweaving, but the resource representing the combination? However, that's not easy when the controllers are Students and Majors, that in effect represent resources of the same name (MVC has really become RV...in the 'convention' approach and not developing Controllers that may have no bearing on a Model name, or the path to reach it) So you'd be deleting a Major or a Student; pick your poison...
How can I avoid managing conditionals across an infinite graph of associated resources where delete really isn't the intent when the delete is desired to be in context of a collection and not pertaining to its singularity...?
...major.student.delete... Is there a way for 'student' ActiveRecord object to know it was sent a 'delete' message in a method chain beginning with the 'major' AR object ?
Well the standard RESTful aproach is to use has_many :through and generate a cotroller for the association resource. Naming association resources is always difficult, but I'll attempt for this example.
resources :majors do
resources :studies
resources :students
end
resources :students do
resources :studies
resources :majors
end
The models would be of course:
class Major < ActiverRecord::Base
has_many :studies
has_many :students, :through => :studies
end
etc. (comment if you want me to elaborate)
Then for a student you wouldn't DELETE it's associated #student.major but his #student.studies.where :major => #major.

Resources