# Query problem

**URL:** <https://www.ruby-forum.com/t/query-problem/146995>\
**Category:** Rails\
**Created:** [August 27, 2008, 11:47am UTC](https://www.ruby-forum.com/t/query-problem/146995 "2008-08-27T11:47:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 27, 2008, 11:47am UTC](https://www.ruby-forum.com/t/query-problem/146995/1 "2008-08-27T11:47:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 27, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/query-problem/146995/2 "2008-08-27T14:40:39Z")

</div>

Sunny B. 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.

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 27, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/query-problem/146995/3 "2008-08-27T14:50:07Z")

</div>

Thanks for this but can you explain brute force in detail? how to use?

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 27, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/query-problem/146995/4 "2008-08-27T15:46:58Z")

</div>

> [1,2,3, “hello”] & [2,‘foo’, ‘hello’,4] #=\> [2, “hello”]
> 
> Fred

Somehow I just _knew_ Fred would have a more elegant solution…

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 27, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/query-problem/146995/5 "2008-08-27T15:25:38Z")

</div>

On 27 Aug 2008, at 13:40, Ar Chron wrote:

> > 
> 
> 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

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 27, 2008, 9:51pm UTC](https://www.ruby-forum.com/t/query-problem/146995/6 "2008-08-27T21:51:04Z")

</div>

Quoting Sunny B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 27, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/query-problem/146995/7 "2008-08-27T16:07:54Z")

</div>

On Wed, Aug 27, 2008 at 8:40 AM, Ar Chron  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > 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](http://www.ruby-doc.org/core/classes/Array.html#M002235)

–  
Tim
