Hi I have a following query in my code. GroupUser.find_by_sql ["select user_id from group_users where group_id =?",6] and is return me [#<GroupUser:0x835c5d4 @attributes={"user_id"=>"1"}>, #<GroupUser:0x835c5ac @attributes={"user_id"=>"6"}>] this how it possible in rails so code return me [1,6] not above array? also it possible to find intersection between to array of objects? Please reply immediate. -- 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 -~----------~----~----~----~------~----~------~--~---
Sunny Bogawat wrote:> Hi I have a following query in my code. > GroupUser.find_by_sql ["select user_id from group_users where group_id > =?",6] and is return me > [#<GroupUser:0x835c5d4 @attributes={"user_id"=>"1"}>, > #<GroupUser:0x835c5ac @attributes={"user_id"=>"6"}>] this > > how it possible in rails so code return me [1,6] not above array? > > also it possible to find intersection between to array of objects? > > Please reply immediate.You should be able to do something like array = GroupUser.find_by_sql(blah blah blah).collect {|group_user| user_id } to get just an array back (or something close to that) To intersect arrays, the brute force method would be to iterate the first array, checking if the element in question is in the second array, if not, remove it. The net result is a list of items in both arrays. -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks for this but can you explain brute force in detail? how to use? -- 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 -~----------~----~----~----~------~----~------~--~---
On 27 Aug 2008, at 13:40, Ar Chron wrote:> > Sunny Bogawat wrote: >> Hi I have a following query in my code. >> GroupUser.find_by_sql ["select user_id from group_users where >> group_id >> =?",6] and is return me >> [#<GroupUser:0x835c5d4 @attributes={"user_id"=>"1"}>, >> #<GroupUser:0x835c5ac @attributes={"user_id"=>"6"}>] this >> >> how it possible in rails so code return me [1,6] not above array? >> >> also it possible to find intersection between to array of objects? >> >> Please reply immediate. > > You should be able to do something like > > array = GroupUser.find_by_sql(blah blah blah).collect {|group_user| > user_id } to get just an array back (or something close to that) > > To intersect arrays, the brute force method would be to iterate the > first array, checking if the element in question is in the second > array, > if not, remove it. The net result is a list of items in both arrays.or [1,2,3, "hello"] & [2,''foo'', ''hello'',4] #=> [2, "hello"] Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---
> > [1,2,3, "hello"] & [2,''foo'', ''hello'',4] #=> [2, "hello"] > > FredSomehow I just *knew* Fred would have a more elegant solution... -- 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 -~----------~----~----~----~------~----~------~--~---
On Wed, Aug 27, 2008 at 8:40 AM, Ar Chron <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > Sunny Bogawat wrote: > > Hi I have a following query in my code. > > GroupUser.find_by_sql ["select user_id from group_users where group_id > > =?",6] and is return me > > [#<GroupUser:0x835c5d4 @attributes={"user_id"=>"1"}>, > > #<GroupUser:0x835c5ac @attributes={"user_id"=>"6"}>] this > > > > how it possible in rails so code return me [1,6] not above array? > > > > also it possible to find intersection between to array of objects? > > > > Please reply immediate. > > You should be able to do something like > > array = GroupUser.find_by_sql(blah blah blah).collect {|group_user| > user_id } to get just an array back (or something close to that)almost... GroupUser.find_by_sql(blah blah blah).collect(&:user_id).> To intersect arrays, the brute force method would be to iterate the > first array, checking if the element in question is in the second array, > if not, remove it. The net result is a list of items in both arrays.Brute force is so... brutish. Do this: (first_array & other_array) http://www.ruby-doc.org/core/classes/Array.html#M002235 -- Tim --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Quoting Sunny Bogawat <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Hi I have a following query in my code. > GroupUser.find_by_sql ["select user_id from group_users where group_id > =?",6] and is return me > [#<GroupUser:0x835c5d4 @attributes={"user_id"=>"1"}>, > #<GroupUser:0x835c5ac @attributes={"user_id"=>"6"}>] this > > how it possible in rails so code return me [1,6] not above array? >sql = "select user_id from group_users where group_id = 6" GroupUser.connection.select_values(sql) without the wordwrap Note, will probably return an array of strings. so... GroupUser.connection.select_values(sql).map{|n| n.to_i} will return an array of integers. HTH, Jeffrey --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---