lunaclaire
2009-Nov-17 03:02 UTC
how to sort on an object contained in a has_many association?
here are my models (only showing the associations that relate to this post): class UpdateRequest < ActiveRecord::Base belongs_to :user belongs_to :contact end class User < ActiveRecord::Base has_many :update_requests, :dependent => :destroy end so I want to be able to list UpdateRequests and sort them based on attributes of the referenced Contact in each I cant, for instance, do the following for the User has_many :update_requests, :dependent => :destroy, :order => ''contacts.last_name asc'' It doesnt know about the ''contacts'' table''s columns I can do this, right? How? Thx
Conrad Taylor
2009-Nov-17 03:36 UTC
Re: how to sort on an object contained in a has_many association?
On Mon, Nov 16, 2009 at 7:02 PM, lunaclaire <szagerman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > here are my models (only showing the associations that relate to this > post): > > class UpdateRequest < ActiveRecord::Base > belongs_to :user > belongs_to :contact > end > > class User < ActiveRecord::Base > has_many :update_requests, :dependent => :destroy > end > > so I want to be able to list UpdateRequests and sort them based on > attributes of the referenced Contact in each > > I cant, for instance, do the following for the User > > has_many :update_requests, :dependent => :destroy, :order => > ''contacts.last_name asc'' > > It doesnt know about the ''contacts'' table''s columns > > I can do this, right? How? > > Thx >How is this to be use? For example, are you displaying this information to the user within the browser? -Conrad> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
lunaclaire
2009-Nov-17 04:14 UTC
Re: how to sort on an object contained in a has_many association?
Yes, in a browser. It''s a page where I list these UpdateRequests showing, amongst other things, the name of the Contact for whom the UpdateRequest has been created. And I''d like to be able to have that list of UpdateRequests sorted on the Contact''s last_name. But, this is really also a general question about how to sort on (and how to get at) the attributes of an associated object from a object that joins objects (note that that''s what an UpdateRequest does for me here). On Nov 16, 7:36 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Nov 16, 2009 at 7:02 PM, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > here are my models (only showing the associations that relate to this > > post): > > > class UpdateRequest < ActiveRecord::Base > > belongs_to :user > > belongs_to :contact > > end > > > class User < ActiveRecord::Base > > has_many :update_requests, :dependent => :destroy > > end > > > so I want to be able to list UpdateRequests and sort them based on > > attributes of the referenced Contact in each > > > I cant, for instance, do the following for the User > > > has_many :update_requests, :dependent => :destroy, :order => > > ''contacts.last_name asc'' > > > It doesnt know about the ''contacts'' table''s columns > > > I can do this, right? How? > > > Thx > > How is this to be use? For example, are you displaying this information to > the user within the > browser? > > -Conrad > >
Tim Harding
2009-Nov-17 05:53 UTC
Re: how to sort on an object contained in a has_many association?
Hi, give this a try. Not sure it will work, haven''t tested it at all, but it might. class User has_many :update_requests has_many :contacts, :through => :update_requests, :order => ''last_name'' end -Tim On Tue, Nov 17, 2009 at 5:14 PM, lunaclaire <szagerman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Yes, in a browser. > > It''s a page where I list these UpdateRequests showing, amongst other > things, the name of the Contact for whom the UpdateRequest has been > created. And I''d like to be able to have that list of UpdateRequests > sorted on the Contact''s last_name. > > But, this is really also a general question about how to sort on (and > how to get at) the attributes of an associated object from a object > that joins objects (note that that''s what an UpdateRequest does for me > here). > > On Nov 16, 7:36 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On Mon, Nov 16, 2009 at 7:02 PM, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > here are my models (only showing the associations that relate to this > > > post): > > > > > class UpdateRequest < ActiveRecord::Base > > > belongs_to :user > > > belongs_to :contact > > > end > > > > > class User < ActiveRecord::Base > > > has_many :update_requests, :dependent => :destroy > > > end > > > > > so I want to be able to list UpdateRequests and sort them based on > > > attributes of the referenced Contact in each > > > > > I cant, for instance, do the following for the User > > > > > has_many :update_requests, :dependent => :destroy, :order => > > > ''contacts.last_name asc'' > > > > > It doesnt know about the ''contacts'' table''s columns > > > > > I can do this, right? How? > > > > > Thx > > > > How is this to be use? For example, are you displaying this information > to > > the user within the > > browser? > > > > -Conrad > > > > > > >-- Tim Harding Muriwai Technologies Ltd Registered in England & Wales Company number 5800651 Registered office: 2 Church St, Burnham, Bucks, SL1 7HZ Well Informed Ltd Registered in England & Wales Company number 06707839 Registered office: Suite 235, 77 Beak St, London, W1F 9DB --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matt Jones
2009-Nov-17 15:27 UTC
Re: how to sort on an object contained in a has_many association?
The easiest way to get the contacts table into the mix is to use :include on the has_many: has_many :update_requests, :dependent => :destroy, :include => :contact, :order => ''contacts.last_name asc'' I''m not 100% sure that this will actually sort the records the way you want, however. If it doesn''t work, you''ll probably need to build a named scope on UpdateRequest that sorts by contact last_name. --Matt Jones On Nov 16, 10:02 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> here are my models (only showing the associations that relate to this > post): > > class UpdateRequest < ActiveRecord::Base > belongs_to :user > belongs_to :contact > end > > class User < ActiveRecord::Base > has_many :update_requests, :dependent => :destroy > end > > so I want to be able to list UpdateRequests and sort them based on > attributes of the referenced Contact in each > > I cant, for instance, do the following for the User > > has_many :update_requests, :dependent => :destroy, :order => > ''contacts.last_name asc'' > > It doesnt know about the ''contacts'' table''s columns > > I can do this, right? How? > > Thx
lunaclaire
2009-Nov-17 18:03 UTC
Re: how to sort on an object contained in a has_many association?
That did work, Matt. Thx! This finally pushes me to learn more about :include, something I havent looked at much in a couple years of Rails work <sheepish grin> Thx again to all On Nov 17, 7:27 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The easiest way to get the contacts table into the mix is to > use :include on the has_many: > > has_many :update_requests, :dependent => :destroy, :include > => :contact, :order => ''contacts.last_name asc'' > > I''m not 100% sure that this will actually sort the records the way you > want, however. If it doesn''t work, you''ll probably need to build a > named scope on UpdateRequest that sorts by contact last_name. > > --Matt Jones > > On Nov 16, 10:02 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > here are my models (only showing the associations that relate to this > > post): > > > class UpdateRequest < ActiveRecord::Base > > belongs_to :user > > belongs_to :contact > > end > > > class User < ActiveRecord::Base > > has_many :update_requests, :dependent => :destroy > > end > > > so I want to be able to list UpdateRequests and sort them based on > > attributes of the referenced Contact in each > > > I cant, for instance, do the following for the User > > > has_many :update_requests, :dependent => :destroy, :order => > > ''contacts.last_name asc'' > > > It doesnt know about the ''contacts'' table''s columns > > > I can do this, right? How? > > > Thx
Frederick Cheung
2009-Nov-17 20:49 UTC
Re: how to sort on an object contained in a has_many association?
On Nov 17, 6:03 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That did work, Matt. Thx! > > This finally pushes me to learn more about :include, something I > havent looked at much in a couple years of Rails work <sheepish grin> >I''d use :joins instead of include here - you''ll actually be loading all those contact objects whenever you load that association, which probably isn''t what you want. Fred> Thx again to all > > On Nov 17, 7:27 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > The easiest way to get the contacts table into the mix is to > > use :include on the has_many: > > > has_many :update_requests, :dependent => :destroy, :include > > => :contact, :order => ''contacts.last_name asc'' > > > I''m not 100% sure that this will actually sort the records the way you > > want, however. If it doesn''t work, you''ll probably need to build a > > named scope on UpdateRequest that sorts by contact last_name. > > > --Matt Jones > > > On Nov 16, 10:02 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > here are my models (only showing the associations that relate to this > > > post): > > > > class UpdateRequest < ActiveRecord::Base > > > belongs_to :user > > > belongs_to :contact > > > end > > > > class User < ActiveRecord::Base > > > has_many :update_requests, :dependent => :destroy > > > end > > > > so I want to be able to list UpdateRequests and sort them based on > > > attributes of the referenced Contact in each > > > > I cant, for instance, do the following for the User > > > > has_many :update_requests, :dependent => :destroy, :order => > > > ''contacts.last_name asc'' > > > > It doesnt know about the ''contacts'' table''s columns > > > > I can do this, right? How? > > > > Thx
Matt Jones
2009-Nov-18 17:32 UTC
Re: how to sort on an object contained in a has_many association?
:joins will work as well - but with the additional gotcha that it (by default) does an inner join, so UpdateRequests without an associated contact won''t get loaded if you use the :joins => :contact syntax. Been bitten by this at least once myself... Also, :include could be useful here anyway, as you''re probably going to want to display at least some of the fields from Contact with the requests (else the sort will look really weird). --Matt Jones On Nov 17, 3:49 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 17, 6:03 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > That did work, Matt. Thx! > > > This finally pushes me to learn more about :include, something I > > havent looked at much in a couple years of Rails work <sheepish grin> > > I''d use :joins instead of include here - you''ll actually be loading > all those contact objects whenever you load that association, which > probably isn''t what you want. > > Fred > > > > > Thx again to all > > > On Nov 17, 7:27 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > The easiest way to get the contacts table into the mix is to > > > use :include on the has_many: > > > > has_many :update_requests, :dependent => :destroy, :include > > > => :contact, :order => ''contacts.last_name asc'' > > > > I''m not 100% sure that this will actually sort the records the way you > > > want, however. If it doesn''t work, you''ll probably need to build a > > > named scope on UpdateRequest that sorts by contact last_name. > > > > --Matt Jones > > > > On Nov 16, 10:02 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > here are my models (only showing the associations that relate to this > > > > post): > > > > > class UpdateRequest < ActiveRecord::Base > > > > belongs_to :user > > > > belongs_to :contact > > > > end > > > > > class User < ActiveRecord::Base > > > > has_many :update_requests, :dependent => :destroy > > > > end > > > > > so I want to be able to list UpdateRequests and sort them based on > > > > attributes of the referenced Contact in each > > > > > I cant, for instance, do the following for the User > > > > > has_many :update_requests, :dependent => :destroy, :order => > > > > ''contacts.last_name asc'' > > > > > It doesnt know about the ''contacts'' table''s columns > > > > > I can do this, right? How? > > > > > Thx-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=.
lunaclaire
2009-Nov-18 22:56 UTC
Re: how to sort on an object contained in a has_many association?
"you''re probably going to want to display at least some of the fields from Contact" exactly... I show the thing I''m sorting on (last_name) along with a number of other attr''s On Nov 18, 9:32 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> :joins will work as well - but with the additional gotcha that it (by > default) does an inner join, so UpdateRequests without an associated > contact won''t get loaded if you use the :joins => :contact syntax. > Been bitten by this at least once myself... > > Also, :include could be useful here anyway, as you''re probably going > to want to display at least some of the fields from Contact with the > requests (else the sort will look really weird). > > --Matt Jones > > On Nov 17, 3:49 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Nov 17, 6:03 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > That did work, Matt. Thx! > > > > This finally pushes me to learn more about :include, something I > > > havent looked at much in a couple years of Rails work <sheepish grin> > > > I''d use :joins instead of include here - you''ll actually be loading > > all those contact objects whenever you load that association, which > > probably isn''t what you want. > > > Fred > > > > Thx again to all > > > > On Nov 17, 7:27 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > The easiest way to get the contacts table into the mix is to > > > > use :include on the has_many: > > > > > has_many :update_requests, :dependent => :destroy, :include > > > > => :contact, :order => ''contacts.last_name asc'' > > > > > I''m not 100% sure that this will actually sort the records the way you > > > > want, however. If it doesn''t work, you''ll probably need to build a > > > > named scope on UpdateRequest that sorts by contact last_name. > > > > > --Matt Jones > > > > > On Nov 16, 10:02 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > here are my models (only showing the associations that relate to this > > > > > post): > > > > > > class UpdateRequest < ActiveRecord::Base > > > > > belongs_to :user > > > > > belongs_to :contact > > > > > end > > > > > > class User < ActiveRecord::Base > > > > > has_many :update_requests, :dependent => :destroy > > > > > end > > > > > > so I want to be able to list UpdateRequests and sort them based on > > > > > attributes of the referenced Contact in each > > > > > > I cant, for instance, do the following for the User > > > > > > has_many :update_requests, :dependent => :destroy, :order => > > > > > ''contacts.last_name asc'' > > > > > > It doesnt know about the ''contacts'' table''s columns > > > > > > I can do this, right? How? > > > > > > Thx-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=.