I have a project going on right now that is really big data model wise. I am trying to figure out the best way to handle inter-model relationships.
For the sake of brevity:
Car
has_many :passengers
has_many :items
or
Team
has_one :head_coach
has_many :coaches
has_many :players
belongs_to :owner
So from the show page I would see who is in the car and what items are in the car. I have some co-workers who think we should have a controller action called manage where they would click a link on the show page to manage the relationship between the other models
So the Team controller would have this
class TeamController < ApplicationController
# ... magic ...
def manage_players
@signed_players = Player.signed_players
@free_agents = Player.free_agents
end
end
The manage_players view would just have links to the actual RESTful actions on the appropriate controller to remove relationships etc…
Anyone have thoughts on how this should be accomplished?
That’s an overly complicated approach, and the good news is, it’s way simpler than you think.
Save yourself some trouble. The quick answer to your question is to use nested resources: you can have a single form that handles the
Carand all the associated passengers/items, or theTeamand its coach, players, etc.The action/view you’re describing would just be the
editaction on theCar/Team. Themanageaction name is a nice idea and all, but the action you’re really taking is an edit (nothing special, by what you’re describing), so why confuse what’s going on when the default is to call itedit?If you want a live example of something that takes advantage of nested routes, check out rpglogger.com (it’s my site). When you play around with it, notice the routes/URLs in the address bar.
It’s also open source. Specifically relevant to your question is:
sectionstwice – this actually gives me two different versions of the routes – one that’s scoped to the LogBook, and one that’s scoped to the objects in a sectionnewandeditform – yet it’s short, rather uncomplicated, and pretty easy to read/undestand given what it does.