# Querying and returning an array rather than an object?

**URL:** <https://www.ruby-forum.com/t/querying-and-returning-an-array-rather-than-an-object/119047>\
**Category:** Rails\
**Created:** [November 2, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/querying-and-returning-an-array-rather-than-an-object/119047 "2007-11-02T15:27:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 2, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/querying-and-returning-an-array-rather-than-an-object/119047/1 "2007-11-02T15:27:50Z")

</div>

I’m trying to write an application where people can vote on things,  
and I want to make sure a person can’t vote twice. Pretend ‘user’ is  
voting on various objects. I was thinking about something like this:

v = user.votes.find(:all, :select =\> ‘object\_id’).

if v.contains?(object\_a.id) then already\_voted\_a  
if v.contains?(object\_b.id) then already\_voted\_b  
if v.contains?(object\_c.id) then already\_voted\_c

etc…

But v won’t be an array of object ids, it will be an array of votes,  
right? How do you think I should go about doing this?

They’re going to be many objects on a single page, so I only want to  
do one database query.

Thanks!

Stedwick

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 2, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/querying-and-returning-an-array-rather-than-an-object/119047/2 "2007-11-02T15:30:46Z")

</div>

On 2 Nov 2007, at 14:27, Stedwick wrote:

> etc…
> 
> But v won’t be an array of object ids, it will be an array of votes,  
> right? How do you think I should go about doing this?
> 
> They’re going to be many objects on a single page, so I only want to  
> do one database query.

Well if you do just want an array of values, you might be interested  
in the select\_all or select\_values methods provided by the connection  
(ie User.connection.select\_values ‘SELECT object\_id from votes where  
user\_id = 123’)

Fred

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 2, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/querying-and-returning-an-array-rather-than-an-object/119047/3 "2007-11-02T15:38:23Z")

</div>

Thanks!

I also realized I could probably use things like:

v.map!{|x| x.object\_id}

or

v.flatten!.compact!

Those would probably work too, right?

On Nov 2, 10:29 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
