jfloyd-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org
2007-Apr-04 17:37 UTC
Order a find using a has_many association
I''m trying to order a find through a has_many association. I have a
model called SalesOrder. Each sales order has things like amount,
description, etc. I use the following to get the Sales Orders:
@sales_orders = SalesOrder.find(:all)
And I display them with:
<td><%= link_to sales_order.id.to_s, :action =>
''show'', :id =>
sales_order.id %></td>
<td><%= sales_order.sold_to %></td>
<td><%= sales_order.ship_to %></td>
<td><%= (sales_order.rep.length < 1 ? " " :
sales_order.rep) %></
td>
<td><%= sales_order.description %></td>
<td class="right_align"><%= sprintf("%#1.2f",
sales_order.amount) %></
td>
<td><%= sales_order.ship_date.strftime(''%m/%d/%y'')
%></td>
<td><%= sales_order.latest_history.nil? ? " " :
sales_order.latest_history.route.name %></td>
The problem is that this list is not in the order that I need it to
be. I can order it by any column in the sales_orders table, but I
need to order it by latest_history.route.name I don''t know how to
order a query based on another table''s column. Is this even
possible? Or is there another way to accomplish the same result? My
model definitions are below:
class SalesOrder < ActiveRecord::Base
has_many :histories
has_one :latest_history,
:class_name => "History",
:order => "id desc"
end
class History < ActiveRecord::Base
belongs_to :route
belongs_to :sales_order
end
class Route < ActiveRecord::Base
has_many :histories
has_and_belongs_to_many :users
end
Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
you might want to try this @sales_orders = SalesOrder.find(:all, :include => [:histories, :routes], :order => "routes.name") -Dutch On 4/4/07, jfloyd-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org <jfloyd-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org> wrote:> > > I''m trying to order a find through a has_many association. I have a > model called SalesOrder. Each sales order has things like amount, > description, etc. I use the following to get the Sales Orders: > > @sales_orders = SalesOrder.find(:all) > > And I display them with: > > <td><%= link_to sales_order.id.to_s, :action => ''show'', :id => > sales_order.id %></td> > <td><%= sales_order.sold_to %></td> > <td><%= sales_order.ship_to %></td> > <td><%= (sales_order.rep.length < 1 ? " " : sales_order.rep) %></ > td> > <td><%= sales_order.description %></td> > <td class="right_align"><%= sprintf("%#1.2f", sales_order.amount) %></ > td> > <td><%= sales_order.ship_date.strftime(''%m/%d/%y'') %></td> > <td><%= sales_order.latest_history.nil? ? " " : > sales_order.latest_history.route.name %></td> > > The problem is that this list is not in the order that I need it to > be. I can order it by any column in the sales_orders table, but I > need to order it by latest_history.route.name I don''t know how to > order a query based on another table''s column. Is this even > possible? Or is there another way to accomplish the same result? My > model definitions are below: > > class SalesOrder < ActiveRecord::Base > has_many :histories > has_one :latest_history, > :class_name => "History", > :order => "id desc" > end > > class History < ActiveRecord::Base > belongs_to :route > belongs_to :sales_order > end > > class Route < ActiveRecord::Base > has_many :histories > has_and_belongs_to_many :users > > end > > > Thanks! > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
jfloyd-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org
2007-Apr-04 18:20 UTC
Re: Order a find using a has_many association
Ok, I added the following to my SalesOrder model: has_many :routes, :through => :histories Then I tried: @sales_orders = SalesOrder.find(:all, :include => [:histories, :routes], :order => ''routes.name'') That partially worked. It seemed to group together all sales orders where the route wasn''t blank. But they weren''t all grouped together by the same status. Here''s the order of the routes they were in: Production Control Application Data Sheet Approval Manufacturing Engineering Released to Shop Manufacturing Engineering Manufacturing Engineering Manufacturing Engineering Manufacturing Engineering Project Engineering Released to Shop Why weren''t the Manufacturing Engineering and Released to Shop routes all together? Thanks! On Apr 4, 12:44 pm, "Dutch Rapley" <dutch.rap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> you might want to try this > > @sales_orders = SalesOrder.find(:all, :include => [:histories, :routes], > :order => "routes.name") > > -Dutch > > On 4/4/07, jfl...-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org <jfl...-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org> wrote: > > > > > I''m trying to order a find through a has_many association. I have a > > model called SalesOrder. Each sales order has things like amount, > > description, etc. I use the following to get the Sales Orders: > > > @sales_orders = SalesOrder.find(:all) > > > And I display them with: > > > <td><%= link_to sales_order.id.to_s, :action => ''show'', :id => > > sales_order.id %></td> > > <td><%= sales_order.sold_to %></td> > > <td><%= sales_order.ship_to %></td> > > <td><%= (sales_order.rep.length < 1 ? " " : sales_order.rep) %></ > > td> > > <td><%= sales_order.description %></td> > > <td class="right_align"><%= sprintf("%#1.2f", sales_order.amount) %></ > > td> > > <td><%= sales_order.ship_date.strftime(''%m/%d/%y'') %></td> > > <td><%= sales_order.latest_history.nil? ? " " : > > sales_order.latest_history.route.name %></td> > > > The problem is that this list is not in the order that I need it to > > be. I can order it by any column in the sales_orders table, but I > > need to order it by latest_history.route.name I don''t know how to > > order a query based on another table''s column. Is this even > > possible? Or is there another way to accomplish the same result? My > > model definitions are below: > > > class SalesOrder < ActiveRecord::Base > > has_many :histories > > has_one :latest_history, > > :class_name => "History", > > :order => "id desc" > > end > > > class History < ActiveRecord::Base > > belongs_to :route > > belongs_to :sales_order > > end > > > class Route < ActiveRecord::Base > > has_many :histories > > has_and_belongs_to_many :users > > > end > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ah, I''m assuming you want to group sub items together, would you mind reponding back with you view code? On 4/4/07, jfloyd-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org <jfloyd-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org> wrote:> > > Ok, > > I added the following to my SalesOrder model: > > has_many :routes, :through => :histories > > Then I tried: > > @sales_orders = SalesOrder.find(:all, :include => > [:histories, :routes], :order => ''routes.name'') > > That partially worked. It seemed to group together all sales orders > where the route wasn''t blank. But they weren''t all grouped together > by the same status. Here''s the order of the routes they were in: > > Production Control > Application Data Sheet Approval > Manufacturing Engineering > Released to Shop > Manufacturing Engineering > Manufacturing Engineering > Manufacturing Engineering > Manufacturing Engineering > Project Engineering > Released to Shop > > Why weren''t the Manufacturing Engineering and Released to Shop routes > all together? > > Thanks! > > > > > > > On Apr 4, 12:44 pm, "Dutch Rapley" <dutch.rap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > you might want to try this > > > > @sales_orders = SalesOrder.find(:all, :include => [:histories, :routes], > > :order => "routes.name") > > > > -Dutch > > > > On 4/4/07, jfl...-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org <jfl...-9RPjh6LLLbEOPmVbiNFCMNBPR1lH4CV8@public.gmane.org> wrote: > > > > > > > > > I''m trying to order a find through a has_many association. I have a > > > model called SalesOrder. Each sales order has things like amount, > > > description, etc. I use the following to get the Sales Orders: > > > > > @sales_orders = SalesOrder.find(:all) > > > > > And I display them with: > > > > > <td><%= link_to sales_order.id.to_s, :action => ''show'', :id => > > > sales_order.id %></td> > > > <td><%= sales_order.sold_to %></td> > > > <td><%= sales_order.ship_to %></td> > > > <td><%= (sales_order.rep.length < 1 ? " " : sales_order.rep) %></ > > > td> > > > <td><%= sales_order.description %></td> > > > <td class="right_align"><%= sprintf("%#1.2f", sales_order.amount) %></ > > > td> > > > <td><%= sales_order.ship_date.strftime(''%m/%d/%y'') %></td> > > > <td><%= sales_order.latest_history.nil? ? " " : > > > sales_order.latest_history.route.name %></td> > > > > > The problem is that this list is not in the order that I need it to > > > be. I can order it by any column in the sales_orders table, but I > > > need to order it by latest_history.route.name I don''t know how to > > > order a query based on another table''s column. Is this even > > > possible? Or is there another way to accomplish the same result? My > > > model definitions are below: > > > > > class SalesOrder < ActiveRecord::Base > > > has_many :histories > > > has_one :latest_history, > > > :class_name => "History", > > > :order => "id desc" > > > end > > > > > class History < ActiveRecord::Base > > > belongs_to :route > > > belongs_to :sales_order > > > end > > > > > class Route < ActiveRecord::Base > > > has_many :histories > > > has_and_belongs_to_many :users > > > > > end > > > > > Thanks! > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Here you go:
<% for sales_order in @sales_orders %>
<tr>
<td><%= sales_order.sold_to %></td>
<td><%= sales_order.ship_to %></td>
<td><%= sales_order.description %></td>
<td><%= sprintf("%#1.2f", sales_order.amount)
%></td>
<td><%=
sales_order.ship_date.strftime(''%m/%d/%y'') %></td>
<!-- this is the column of the table that''s not sorting right
-->
<td><%= sales_order.latest_history.nil? ? " " :
sales_order.latest_history.route.name %></td>
</tr>
--
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
-~----------~----~----~----~------~----~------~--~---