Forum: Ruby on Rails serialize array finder

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6fa76b3113f66ea5055ad3f10ab92819?d=identicon&s=25 Grayson Piercee (grayson)
on 2008-12-11 03:05
Hello,

Not sure if this is even possible/practical but I'd like to serialize an
array to a string field like this and then be able to match against it.

User.find(:all, :conditions => ["interests in
(?)",current_user.interests)

Am I better of not denormalizing?  How would this be done with ordinary
A/R relationships because I'm not looking for an exact match, I'm
looking for any interests that the users have in common (more like a
ranking)

GP
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-12-11 03:18
(Received via mailing list)
Grayson Piercee wrote:

> User.find(:all, :conditions => ["interests in
> (?)",current_user.interests)

Here's a wild guess. I hope someone else corrects it if I have lead you
astray!

User.find(:all, :include => :interests,
   :conditions => ["interests.id IN (?)",current_user.interests]
   :order => 'COUNT(interests.id) DESC')

(At work we have taken to putting all raw SQL commands in ALL CAPS...)
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-12-12 04:06
(Received via mailing list)
Phlip wrote:
> Grayson Piercee wrote:
>
>> User.find(:all, :conditions => ["interests in
>> (?)",current_user.interests)
>
> Here's a wild guess. I hope someone else corrects it if I have lead you astray!

such as...

  User.find(:all, :include => :interests,
     :conditions => [ "interests.id IN (?)",
                       current_user.interests.map(&:id) ]
     :order => 'COUNT(interests.id) DESC')

and use inspect_sql to see the query it generates!
This topic is locked and can not be replied to.