Ruby on Rails Sunday, September 5, 2010

Trying to convert my app (and brain) to RESTful routes from the old
style. What is the accepted approach to nesting a resource within 2
other resources?

Example:

Models

Person has_many :scores
Contest has_many :scores
Score belongs_to :person, :contest

Routes

resources :people do
resources :scores
end

resources :competitions do
resources :scores
end

I want to be able to list scores by person, or by competition. Either
of the these two paths will request the :index action in the :scores
controller:

GET /people/person_id/scores
GET /contests/contest_id/scores

So what is the best way to structure scores/index to deliver the
appropriate list?

I could test for the presence of params[person_id] or
params[contest_id], then execute and render accordingly within
the :index action. But that seems somewhat inelegant. Is there a
better way? Should I have two separate actions within the :score
controller, and modify my route mapping somehow to request the
appropriate action?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment