My app calls for the use of :foreign_key into a model that it should be nested with. I can''t get the routes set up the way I want them. Here is an example: People buy and sell from each other. The purchase or sale is recorded in an Order model. class Person < ActiveRecord::Base has_many :purchases, :class_name => ''Order'', :foreign_key => ''buyer_id'' has_many :sales, :class_name => ''Order'', :foreign_key => ''seller_id'' ... class Order < ActiveRecord::Base belongs_to :seller, :class_name => "Person" belongs_to :buyer, :class_name => "Person" ... The routes to display a list of purchases or sales is /people/1/orders? buyer_id=1 and /people/1/orders?seller_id=1 respectively. The route table has this entry: map.resources :people, :has_many => :orders The Orders controller ferrets out which parameter was sent in and does the appropriate search before rendering the index. (At this point, I feel all sticky and want to take a shower.) I''d really like to have nested routes such as /people/1/purchases and / people/1/sales. So I''m kind of surprised I can''t do this in Rails 2.3.8: map.resources :people do |people| people.resource :sales, :as => ''orders'' people.resource :purchases, :as => ''orders'' end ... or something similar to give me the correct nested routes. Maybe you have a better way? Maybe I''m missing something. Thanks, d. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Nichols
2010-Jul-06 10:55 UTC
Re: Nested routes with has_many associations via a :foreign_key
Dee wrote:> > I''d really like to have nested routes such as /people/1/purchases and / > people/1/sales. So I''m kind of surprised I can''t do this in RailsYou can do it with map.connect. You may have to play with the syntax, but I think this will work: map.connect ''people/:person_id/:controller'', :action => ''index'', :person_id => /\d+/ That will make ''people/1/sales'' the equivalent of ''sales?person_id=1'' You''ll then need to update you index action in your sales controller, to detect params[:person_id] and use it to modify @sales. Or you can create a custom action to handle this. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.