Forum: Ruby on Rails Avoid deeply nested routes for many related resources

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
51cdfc97866048aeb30af8c6abe70168?d=identicon&s=25 Serafino P. (serpico)
on 2013-10-02 12:30
Hi all,

I'm having some headaches on how to setup my routes for a small app I'm
developing. The app has to store the results of a game I'm doing with
some friends.
I have 4 models, defined as follows:

Match: has_many :scores
Team: has_many :players
Player: has_many :scores ; belongs_to :team
Score: belongs_to :player ; belongs_to :match

These are currently my routes:

  resources :teams, shallow: true do
    resources :players do
      resources :scores, only: [:index]

  resources :matches

This allows the administrator to manage teams, players, and view the
scores associated with a singla player. I can also display a list of all
matches. Simple enough.

The problem now is that I need some players, the leaders of each team,
to see and edit the results for their team for a specific match: once
they select a match the list at /matches/, i need to show them a list of
all the players of their team with their score (if present) for that
specific match, and from there allow them to edit or insert a score for
each player (one at a time).

I'm really having troubles defining the routes for this, because there
are several models involved in the creation of a new score but I want to
avoid nesting resources too much.

How would you define routes for a situation like this one? Consider that
the team can be found from the logged user data, so, at least for that,
there's no need to pass the team id around.

If you have any question please ask, I'm sorry if maybe it's not too
clear but english is not my native language.
This topic is locked and can not be replied to.