I have 3 models, requests, listings, and users. requests belong to listings and users listings belong to users I have the associations all properly set up, so that is not the problem What I am trying to do is sort a list of requests by requests.listings.users My find (that doesn''t work) is similar to this. Request.find(:all, :include => [:listing, :user], :order => " requests.listings.user.username", :limit => 20) Some things I''ve thought about... listing and request both belong to the same users table, so my include is probably fine. But I know that my order statement is wrong... to sort by requests.users.username I just use " users.username", but for requests.listings.users.username I can''t use " listings.users.username" because it doesn''t work, and "users.username" isn''t sorting by the right thing. Any clues? Thanks in advance, --Tyler Prete --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/15/06, Tyler Prete <psyonic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have 3 models, requests, listings, and users. > > requests belong to listings and users > listings belong to users > > I have the associations all properly set up, so that is not the problem > > What I am trying to do is sort a list of requests by > requests.listings.users > > My find (that doesn''t work) is similar to this. > > Request.find(:all, :include => [:listing, :user], :order => " > requests.listings.user.username", :limit => 20) >I think the include should be :include => { :listing => :user } I''m not 100% on that though. Some things I''ve thought about... listing and request both belong to the> same users table, so my include is probably fine. But I know that my order > statement is wrong... to sort by requests.users.username I just use " > users.username", but for requests.listings.users.username I can''t use " > listings.users.username" because it doesn''t work, and "users.username" > isn''t sorting by the right thing. Any clues? >The order is just an sql order option. If you want it to sort by listings then usernames, use something like :order => "listings.created_at, users.username" Hope that helps --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Well here is the thing... the conditions of this find statement are such that sometimes it might need to include the requests users, and sometimes it might need the listings users, so I think I might need :include => [{ :listing => :user }, :user]. That leaves me with a problem though... how do I differentiate between the two when giving an order statement? And does :listing => :user also include the listing data? As for using :listing.created_on, :users.username. This doesn''t work because I don''t want it sorted by the Listing users username. Let me try to give an example to explain better. Say I have four users, Adam, Bill, Cody, and Dan. A request belongs to a listing and a user, and a listing belongs to a user. So lets say I have 3 requests All 3 requests belong to Dan. But they all belong to seperate listings, which belong to Adam, Bill, and Cody, respectively. (Note, listings could also belong to Dan, they just don''t to keep the example simple) I have these requests displayed in a table with sortable headers. One of which is Request User, which are all Dan in this case. Another is Listing User. This is what I want to sort by. So it should either sort Adam, Bill, Cody, or Cody, Bill, Adam, depending on whether its ascending or descending. However, to make things easy on myself (I have a lot of headers), all of these have to fit into the same find statement, with only the order different, which is why the :include must be the same. Perhaps aliases would help? Thanks for the help so far, Tyler Prete On 9/14/06, Daniel N <has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On 9/15/06, Tyler Prete <psyonic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I have 3 models, requests, listings, and users. > > > > requests belong to listings and users > > listings belong to users > > > > I have the associations all properly set up, so that is not the problem > > > > What I am trying to do is sort a list of requests by > > requests.listings.users > > > > My find (that doesn''t work) is similar to this. > > > > Request.find(:all, :include => [:listing, :user], :order => " > > requests.listings.user.username", :limit => 20) > > > > I think the include should be > :include => { :listing => :user } > > I''m not 100% on that though. > > > Some things I''ve thought about... listing and request both belong to the > > same users table, so my include is probably fine. But I know that my order > > statement is wrong... to sort by requests.users.username I just use " > > users.username", but for requests.listings.users.username I can''t use " > > listings.users.username" because it doesn''t work, and "users.username" > > isn''t sorting by the right thing. Any clues? > > > > The order is just an sql order option. If you want it to sort by listings > then usernames, use something like > > :order => "listings.created_at, users.username" > > Hope that helps > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Tyler, Hi Tyler, Tyler Prete wrote:> I have the associations all properly set up, > so that is not the problemPlease don''t take this the wrong way, but I couldn''t help but respond when I saw this. I can''t begin to count the number of times I''ve said something like that and then lived to regret it. My first CS prof. taught us a ''dittie'' that''s served me well. ------------------------------ I hate this damned computer. I think I''m gonna sell it. It never does just what I want. Only what I tell it. ------------------------------ Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
BIll, That made me laugh :) I appreciate the humor, and I get your point. There is always a chance that I screwed up somewhere before and its finally come back to haunt me. In this case, however, that doesn''t seem (to me at least) to be the problem. Thanks for the laugh, perhaps it will help clear my head! Tyler Prete On 9/14/06, Bill Walton <bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> > > Hi Tyler, > > Hi Tyler, > > Tyler Prete wrote: > > > I have the associations all properly set up, > > so that is not the problem > > Please don''t take this the wrong way, but I couldn''t help but respond when > I > saw this. I can''t begin to count the number of times I''ve said something > like that and then lived to regret it. My first CS prof. taught us a > ''dittie'' that''s served me well. > > ------------------------------ > I hate this damned computer. > I think I''m gonna sell it. > It never does just what I want. > Only what I tell it. > ------------------------------ > > Best regards, > Bill > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
johnson_d-j9pdmedNgrk@public.gmane.org
2006-Sep-15 12:10 UTC
Re: Complicated find :include problem
Tyler Prete wrote:> Well here is the thing... the conditions of this find statement are such > that sometimes it might need to include the requests users, and sometimes it > might need the listings users, so I think I might need :include => [{ > :listing => :user }, :user]. That leaves me with a problem though... how do > I differentiate between the two when giving an order statement? And does > :listing => :user also include the listing data? > > As for using :listing.created_on, :users.username. > > This doesn''t work because I don''t want it sorted by the Listing users > username. > > Let me try to give an example to explain better. Say I have four users, > Adam, Bill, Cody, and Dan. > A request belongs to a listing and a user, and a listing belongs to a user. > So lets say I have 3 requests > All 3 requests belong to Dan. But they all belong to seperate listings, > which belong to Adam, Bill, and Cody, respectively. > (Note, listings could also belong to Dan, they just don''t to keep the > example simple) > > I have these requests displayed in a table with sortable headers. One of > which is Request User, which are all Dan in this case. > Another is Listing User. This is what I want to sort by. So it should > either sort Adam, Bill, Cody, or > Cody, Bill, Adam, depending on whether its ascending or descending. > However, to make things easy on myself (I have a lot of headers), > all of these have to fit into the same find statement, with only the order > different, which is why the :include must be the same. > > Perhaps aliases would help? > > Thanks for the help so far, > Tyler Prete > > On 9/14/06, Daniel N <has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > On 9/15/06, Tyler Prete <psyonic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > I have 3 models, requests, listings, and users. > > > > > > requests belong to listings and users > > > listings belong to users > > > > > > I have the associations all properly set up, so that is not the problem > > > > > > What I am trying to do is sort a list of requests by > > > requests.listings.users > > > > > > My find (that doesn''t work) is similar to this. > > > > > > Request.find(:all, :include => [:listing, :user], :order => " > > > requests.listings.user.username", :limit => 20) > > > > > > > I think the include should be > > :include => { :listing => :user } > > > > I''m not 100% on that though. > > > > > > Some things I''ve thought about... listing and request both belong to the > > > same users table, so my include is probably fine. But I know that my order > > > statement is wrong... to sort by requests.users.username I just use " > > > users.username", but for requests.listings.users.username I can''t use " > > > listings.users.username" because it doesn''t work, and "users.username" > > > isn''t sorting by the right thing. Any clues? > > > > > > > The order is just an sql order option. If you want it to sort by listings > > then usernames, use something like > > > > :order => "listings.created_at, users.username" > > > > Hope that helps > > > > > > > > > > > ------=_Part_60640_30085674.1158285582666 > Content-Type: text/html; charset=ISO-8859-1 > X-Google-AttachSize: 4010 > > Well here is the thing... the conditions of this find statement are such that sometimes it might need to include the requests users, and sometimes it might need the listings users, so I think I might need :include => [{ :listing => :user }, :user]. That leaves me with a problem though... how do I differentiate between the two when giving an order statement? And does :listing => :user also include the listing data? > <br><br>As for using :listing.created_on, :users.username.<br><br> This doesn''t work because I don''t want it sorted by the Listing users username.<br><br>Let me try to give an example to explain better. Say I have four users, Adam, Bill, Cody, and Dan. > <br>A request belongs to a listing and a user, and a listing belongs to a user. So lets say I have 3 requests<br>All 3 requests belong to Dan. But they all belong to seperate listings, which belong to Adam, Bill, and Cody, respectively. > <br>(Note, listings could also belong to Dan, they just don''t to keep the example simple)<br><br>I have these requests displayed in a table with sortable headers. One of which is Request User, which are all Dan in this case. > <br>Another is Listing User. This is what I want to sort by. So it should either sort Adam, Bill, Cody, or<br>Cody, Bill, Adam, depending on whether its ascending or descending.<br>However, to make things easy on myself (I have a lot of headers), > <br>all of these have to fit into the same find statement, with only the order different, which is why the :include must be the same.<br><br><div><span class="gmail_quote">Perhaps aliases would help?<br><br>Thanks for the help so far, > <br>Tyler Prete<br><br>On 9/14/06, <b class="gmail_sendername">Daniel N</b> <<a href="mailto:has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org">has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> > <div><br><br><div></div><div><span class="q"><span class="gmail_quote">On 9/15/06, <b class="gmail_sendername">Tyler Prete</b> <<a href="mailto:psyonic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> > psyonic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> > <div>I have 3 models, requests, listings, and users.<br><br>requests belong to listings and users<br>listings belong to users<br><br>I have the associations all properly set up, so that is not the problem<br><br>What I am trying to do is sort a list of requests by > requests.listings.users<br><br>My find (that doesn''t work) is similar to this.<br><br>Request.find(:all, :include => [:listing, :user], :order => "requests.listings.user.username", :limit => 20)</div></blockquote> > > </span></div><div><div><br> > I think the include should be <br> > :include => { :listing => :user }<br> > </div><br> > I''m not 100% on that though.</div><div><span class="q"><br> > <br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div>Some things I''ve thought about... listing and request both belong > to the same users table, so my include is probably fine. But I > know that my order statement is wrong... to sort by > requests.users.username I just use " > users.username", but for requests.listings.users.username I can''t > use "listings.users.username" because it doesn''t work, and > "users.username" isn''t sorting by the right thing. Any clues?</div></blockquote></span></div><div><div><br> > The order is just an sql order option. If you want it to sort by listings then usernames, use something like<br> > <br> > :order => "listings.created_at, users.username" <br> > </div><br> > Hope that helps<br></div></div><div><span class="e" id="q_10dae7251e6f3449_5"><br> > <br> > <br> > </span></div></blockquote></div><br> > > ------=_Part_60640_30085674.1158285582666--I would drop into straight SQL at this point so that there was no ambiguity. Rails is slick, but it''s not magic. If the concept is easier to express in SQL use that. It also sounds like you have two distinct pieces of business, requiring two distinct "find" statements. Conditional logic at this level is generally a sign of a misunderstanding, in my experience. Without understanding the business problem driving this possibly conditional query, I find it difficult to offer a concrete solution. Can you please explain what you want in business (not technical) terms, submit the DDL for the tables and explain the relationships? We might be able to offer better advice. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > johnson_d-j9pdmedNgrk@public.gmane.org said: > I would drop into straight SQL at this point so that there was no >ambiguity. Rails is slick, but it''s not magic. If the concept is> easier to express in SQL use that.I agree here. Sometimes, when SQL statements get too complex, find_by_sql is your best bet. Anytime I have a query that runs often and/or has several joins, I use find_by_sql. Rails'' SQL builder isn''t always the most efficient. ed --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---