straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-04 18:32 UTC
add column to find
I have a find with a has_many relationship using :include for the relationship. My question is how do i add an additional column to the query and keep all the associations in tact? I need to add a distance column in the output for some geocoding but can not figure out how to do this without being forced to find_by_sql. Any ideas would be greatly appreciated. Thanks Adam --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''ll assume that the "column" in question isn''t in any table but being computed on the fly. If so, simply modify the '':select =>'' parameter to add your additional column. I.e. Model.find(:all, :select => ''*, insert_ugly_trig_formula_here AS distance'', :conditions => etc. etc.) Notice the ''*,'' so that you still get the other columns you (and ActiveRecord) normally expect. Once the query returns you''ll have an @object.distance method you can call that gives you the computed distance for that record. straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I have a find with a has_many relationship using :include for the > relationship. My question is how do i add an additional column to the query > and keep all the associations in tact? I need to add a distance column in > the output for some geocoding but can not figure out how to do this without > being forced to find_by_sql. > > Any ideas would be greatly appreciated. > > Thanks > Adam > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-04 20:55 UTC
Re: add column to find
Thanks Jon. Does this work as well with eager loading ? Thats why I included that i was using :include as well in my find. Just want to make sure that is not going to throw this in a hissy, because i have been trying :select with eager loading and not having any luck. Thanks Adam On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''ll assume that the "column" in question isn''t in any table but being > computed on the fly. If so, simply modify the '':select =>'' parameter to > add your additional column. I.e. > > Model.find(:all, :select => ''*, insert_ugly_trig_formula_here AS > distance'', :conditions => etc. etc.) > > Notice the ''*,'' so that you still get the other columns you (and > ActiveRecord) normally expect. Once the query returns you''ll have an > @object.distance method you can call that gives you the computed > distance for that record. > > > > straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > I have a find with a has_many relationship using :include for the > > relationship. My question is how do i add an additional column to the > query > > and keep all the associations in tact? I need to add a distance column > in > > the output for some geocoding but can not figure out how to do this > without > > being forced to find_by_sql. > > > > Any ideas would be greatly appreciated. > > > > Thanks > > Adam > > > > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Actually, no. Eager loading with :include => overwrites anything you put in the :select => parameter, so you can''t do both. Sorry, I didn''t pick up on the importance of the eager loading in your original question. straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Thanks Jon. > > Does this work as well with eager loading ? Thats why I included that i > was using :include as well in my find. Just want to make sure that is not > going to throw this in a hissy, because i have been trying :select with > eager loading and not having any luck. > > Thanks > Adam > > On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I''ll assume that the "column" in question isn''t in any table but being >> computed on the fly. If so, simply modify the '':select =>'' parameter to >> add your additional column. I.e. >> >> Model.find(:all, :select => ''*, insert_ugly_trig_formula_here AS >> distance'', :conditions => etc. etc.) >> >> Notice the ''*,'' so that you still get the other columns you (and >> ActiveRecord) normally expect. Once the query returns you''ll have an >> @object.distance method you can call that gives you the computed >> distance for that record. >> >> >> >> straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> >>> I have a find with a has_many relationship using :include for the >>> relationship. My question is how do i add an additional column to the >>> >> query >> >>> and keep all the associations in tact? I need to add a distance column >>> >> in >> >>> the output for some geocoding but can not figure out how to do this >>> >> without >> >>> being forced to find_by_sql. >>> >>> Any ideas would be greatly appreciated. >>> >>> Thanks >>> Adam >>> >>> >>> >> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-04 22:02 UTC
Re: add column to find
Thats what I thought. Any suggestions on a workaround? The only other thing I was thinking was somehow using a postgresql function or view to make an additional column available at the postgres level. Thanks again for the help. Adam On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Actually, no. Eager loading with :include => overwrites anything you put > in the :select => parameter, so you can''t do both. Sorry, I didn''t pick up > on the importance of the eager loading in your original question. > > > straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Thanks Jon. > > Does this work as well with eager loading ? Thats why I included that i > was using :include as well in my find. Just want to make sure that is not > going to throw this in a hissy, because i have been trying :select with > eager loading and not having any luck. > > Thanks > Adam > > On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''ll assume that the "column" in question isn''t in any table but being > computed on the fly. If so, simply modify the '':select =>'' parameter to > add your additional column. I.e. > > Model.find(:all, :select => ''*, insert_ugly_trig_formula_here AS > distance'', :conditions => etc. etc.) > > Notice the ''*,'' so that you still get the other columns you (and > ActiveRecord) normally expect. Once the query returns you''ll have an > @object.distance method you can call that gives you the computed > distance for that record. > > > > straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > I have a find with a has_many relationship using :include for the > relationship. My question is how do i add an additional column to the > > query > > and keep all the associations in tact? I need to add a distance column > > in > > the output for some geocoding but can not figure out how to do this > > without > > being forced to find_by_sql. > > Any ideas would be greatly appreciated. > > Thanks > Adam > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The one time I''ve tried to do this, I didn''t actually need to eager load, I just needed to include other tables in the SELECT function and the WHERE conditions, so I used :joins => instead of :include => and that worked well for me. That may not work for you though. Your other options are, A) not eager load, or B) compute ''distance'' for each item later on in a separate method. straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Thats what I thought. Any suggestions on a workaround? The only other > thing I was thinking was somehow using a postgresql function or view to make > an additional column available at the postgres level. > > Thanks again for the help. > Adam > > > On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Actually, no. Eager loading with :include => overwrites anything you put >> in the :select => parameter, so you can''t do both. Sorry, I didn''t pick up >> on the importance of the eager loading in your original question. >> >> >> straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> >> Thanks Jon. >> >> Does this work as well with eager loading ? Thats why I included that i >> was using :include as well in my find. Just want to make sure that is not >> going to throw this in a hissy, because i have been trying :select with >> eager loading and not having any luck. >> >> Thanks >> Adam >> >> On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> I''ll assume that the "column" in question isn''t in any table but being >> computed on the fly. If so, simply modify the '':select =>'' parameter to >> add your additional column. I.e. >> >> Model.find(:all, :select => ''*, insert_ugly_trig_formula_here AS >> distance'', :conditions => etc. etc.) >> >> Notice the ''*,'' so that you still get the other columns you (and >> ActiveRecord) normally expect. Once the query returns you''ll have an >> @object.distance method you can call that gives you the computed >> distance for that record. >> >> >> >> straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> >> I have a find with a has_many relationship using :include for the >> relationship. My question is how do i add an additional column to the >> >> query >> >> and keep all the associations in tact? I need to add a distance column >> >> in >> >> the output for some geocoding but can not figure out how to do this >> >> without >> >> being forced to find_by_sql. >> >> Any ideas would be greatly appreciated. >> >> Thanks >> Adam >> >> >> >> >> >> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-06 02:48 UTC
Re: add column to find
Jon, So does that mean i can use :select with :joins , but not :include ? The function idea is a good recommendation, but I unfortunately need to sort the sql query by distance . Thanks Adam On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > The one time I''ve tried to do this, I didn''t actually need to eager load, > I just needed to include other tables in the SELECT function and the WHERE > conditions, so I used :joins => instead of :include => and that worked well > for me. That may not work for you though. Your other options are, A) not > eager load, or B) compute ''distance'' for each item later on in a separate > method. > > straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Thats what I thought. Any suggestions on a workaround? The only other > thing I was thinking was somehow using a postgresql function or view to make > an additional column available at the postgres level. > > Thanks again for the help. > Adam > > > On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Actually, no. Eager loading with :include => overwrites anything you put > in the :select => parameter, so you can''t do both. Sorry, I didn''t pick up > on the importance of the eager loading in your original question. > > > straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Thanks Jon. > > Does this work as well with eager loading ? Thats why I included that i > was using :include as well in my find. Just want to make sure that is not > going to throw this in a hissy, because i have been trying :select with > eager loading and not having any luck. > > Thanks > Adam > > On 12/4/06, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''ll assume that the "column" in question isn''t in any table but being > computed on the fly. If so, simply modify the '':select =>'' parameter to > add your additional column. I.e. > > Model.find(:all, :select => ''*, insert_ugly_trig_formula_here AS > distance'', :conditions => etc. etc.) > > Notice the ''*,'' so that you still get the other columns you (and > ActiveRecord) normally expect. Once the query returns you''ll have an > @object.distance method you can call that gives you the computed > distance for that record. > > > > straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > I have a find with a has_many relationship using :include for the > relationship. My question is how do i add an additional column to the > > query > > and keep all the associations in tact? I need to add a distance column > > in > > the output for some geocoding but can not figure out how to do this > > without > > being forced to find_by_sql. > > Any ideas would be greatly appreciated. > > Thanks > Adam > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yes, :select and :joins play very nice together. straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Jon, > > So does that mean i can use :select with :joins , but not :include ? > > The function idea is a good recommendation, but I unfortunately need to > sort the sql query by distance . > > Thanks > Adam > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---