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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Nov-02 14:29 UTC
Re: Querying and returning an array rather than an object?
On 2 Nov 2007, at 14:27, Stedwick wrote:> > 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. >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 --~--~---------~--~----~------------~-------~--~----~ 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!
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 Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 2 Nov 2007, at 14:27, Stedwick wrote:
>
>
>
>
>
> > 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.
>
> 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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---