Query problem

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.

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.

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

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

[1,2,3, "hello"] & [2,'foo', 'hello',4] #=> [2, "hello"]

Fred

Somehow I just *knew* Fred would have a more elegant solution...

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

Quoting Sunny Bogawat <rails-mailing-list@andreas-s.net>:

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