Corey Murphy
2009-Jan-08 16:10 UTC
Trying to sort results of one-to-many relationship find
Assuming I have two models: Order "belongs_to :payee" Payee "has_many :orders" In the orders table, I store the payee_id. If I want to pull all orders and display the list alphabetically by the payees last name, what would be the best way to do it? Would I need to setup a true "join" table and then run my find on it? I can''t do this through the view using a sort plugin since that simply changes the finders SQL and doesn''t include the association logic needed here. Since I''m accessing the payee''s name through the association "order.payee.last_name" what is the best way to accomplish what I need or do I need to write some convoluted method that breaks down the recordset and orders it manually? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This is a plain association, no join table involved.
Try this in script/console and see if it solves your problem. The DB
can easily take care of the sorting:
orders = Order.find(:all, :conditions => ''...'', :include
=> :payee, :order => ''payees.last_name'')
Then,
orders.each { |o| puts o.payee.last_name }
-H
On Jan 8, 11:10 am, Corey Murphy
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Assuming I have two models:
>
> Order "belongs_to :payee"
> Payee "has_many :orders"
>
> In the orders table, I store the payee_id.
>
> If I want to pull all orders and display the list alphabetically by the
> payees last name, what would be the best way to do it?
>
> Would I need to setup a true "join" table and then run my find on
it? I
> can''t do this through the view using a sort plugin since that
simply
> changes the finders SQL and doesn''t include the association logic
needed
> here.
>
> Since I''m accessing the payee''s name through the
association
> "order.payee.last_name" what is the best way to accomplish what I
need
> or do I need to write some convoluted method that breaks down the
> recordset and orders it manually?
> --
> Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
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
-~----------~----~----~----~------~----~------~--~---
Corey Murphy
2009-Jan-08 18:42 UTC
Re: Trying to sort results of one-to-many relationship find
Harold wrote:> orders = Order.find(:all, :conditions => ''...'', :include > => :payee, :order => ''payees.last_name'')Awesome. The ":include" option wasn''t something I was aware you could use in a find for the model. I knew this could be handled using a nice rails-like convention without having to code some convoluted sorting method. That makes life much easier. Thank you very much! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---