My problem is similar to http://www.ruby-forum.com/topic/171399, but I don''t understand how the asker solved the problem. I have a model called issues. It returns results that contain a field called "status". The results might look like this: open open closed in progress open open How can I use the "uniq" method on the model in order to return a unique list of statuses like this: open closed in progress -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
In addition to my question above, I''d also like to apply the equivalent of a ''where'' clause without forcing another SQL query, in order to get all the columns for "issues" where "status=open". The results would be: 1, open, fred 2, open, john 5, open, john 6, open, sara -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Franz Strebel
2010-Apr-15 08:01 UTC
Re: Re: Find distinct values in an array of database results
On Wed, Apr 14, 2010 at 11:14 PM, joe mejoe <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> In addition to my question above, I''d also like to apply the equivalent > of a ''where'' clause without forcing another SQL query, in order to get# modify this as needed results = Issue.find(...) statuses = results.collect(&:status).uniq.sort open_issues = results.collect { |i| i.status == ''open'' } I suggest you read up on the Array class and its methods collect, uniq and sort which I used here. Have fun. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Christophe Decaux
2010-Apr-15 08:20 UTC
Re: Re: Find distinct values in an array of database results
If I may jump in, I''m interested in understanding why you would do this kind of database job with Ruby vs. an extra SQL query. I had the feeling that it would be wiser (and would execute faster) to delegate the job to the database engine But I''m kind of amateur. Thanks to anyone who is willing to put some light for me Christophe Le 15 avr. 2010 à 10:01, Franz Strebel a écrit :> On Wed, Apr 14, 2010 at 11:14 PM, joe mejoe <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> In addition to my question above, I''d also like to apply the equivalent >> of a ''where'' clause without forcing another SQL query, in order to get > > # modify this as needed > results = Issue.find(...) > > statuses = results.collect(&:status).uniq.sort > > open_issues = results.collect { |i| i.status == ''open'' } > > I suggest you read up on the Array class and its methods > collect, uniq and sort which I used here. > > Have fun. > > -- > 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=en. >-- 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=en.
Marnen Laibow-Koser
2010-Apr-15 11:15 UTC
Re: Re: Find distinct values in an array of database results
Christophe Decaux wrote:> If I may jump in, I''m interested in understanding why you would do this > kind of database job with Ruby vs. an extra SQL query. > I had the feeling that it would be wiser (and would execute faster) to > delegate the job to the database engineI agree. This is a job for SELECT DISTINCT, unless you''ve already got the recordset stored from a previous operation. In general, do your DB queries on the DB side.> > But I''m kind of amateur. > > Thanks to anyone who is willing to put some light for meLight: many Rails developers don''t know how to use a DB properly. :)> > Christophe > > Le 15 avr. 2010 � 10:01, Franz Strebel a �crit :Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/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=en.
Sometimes you''re not querying a database (for example a REST service that returns a json result). Specially in web applications. On Apr 15, 12:15 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Christophe Decaux wrote: > > If I may jump in, I''m interested in understanding why you would do this > > kind of database job with Ruby vs. an extra SQL query. > > I had the feeling that it would be wiser (and would execute faster) to > > delegate the job to the database engine > > I agree. This is a job for SELECT DISTINCT, unless you''ve already got > the recordset stored from a previous operation. > > In general, do your DB queries on the DB side. > > > > > But I''m kind of amateur. > > > Thanks to anyone who is willing to put some light for me > > Light: many Rails developers don''t know how to use a DB properly. :) > > > > > Christophe > > > Le 15 avr. 2010 10:01, Franz Strebel a crit : > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > 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-/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=en.
Marnen Laibow-Koser
2010-Apr-26 20:42 UTC
Re: Find distinct values in an array of database results
[Please quote when replying, so we know what in particular you''re responding to.] Vincent M. wrote:> Sometimes you''re not querying a database (for example a REST service > that returns a json result). Specially in web applications.True. But where you have a database, you should use it. :) Especially in Web applications? I don''t know. I think I''ve used more non-Web apps that query Web services than Web apps that do likewise. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Franz Strebel
2010-Apr-26 20:50 UTC
Re: Re: Find distinct values in an array of database results
On Mon, Apr 26, 2010 at 10:42 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> [Please quote when replying, so we know what in particular you''re > responding to.] > > Vincent M. wrote: >> Sometimes you''re not querying a database (for example a REST service >> that returns a json result). Specially in web applications. > > True. But where you have a database, you should use it. :)Sure, however, the original poster specifically wanted to avoid querying the database twice, which is why the solution ended up Ruby side. -- 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=en.
Marnen Laibow-Koser
2010-Apr-27 00:02 UTC
Re: Re: Find distinct values in an array of database results
Franz Strebel wrote:> On Mon, Apr 26, 2010 at 10:42 PM, Marnen Laibow-Koser > <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> [Please quote when replying, so we know what in particular you''re >> responding to.] >> >> Vincent M. wrote: >>> Sometimes you''re not querying a database (for example a REST service >>> that returns a json result). Specially in web applications. >> >> True. �But where you have a database, you should use it. :) > > Sure, however, the original poster specifically wanted to avoid querying > the > database twice, which is why the solution ended up Ruby side.As far as I can tell, the OP asked two unrelated questions. I only answered one of them. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/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=en.