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
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
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. [email protected]