简体   繁体   中英

Can I do an 'in' search with searchlogic/metasearch in Rails 3 passing an array?

I am using the acts_as_network gem which allows me to get all the friends for a User through 'User.friends'

I want to create a 'friend feed' showing all the recent events for all of the friends by searching through the Event records where Event:

  Event  | giver_id | receiver_id | date |

Conceptually I'd like to be able to do this:

feed = Events.giver_id_or_receiver_id_in(User.friends).date_gt(Date.today.2.weeks.ago)

This should give me an array of all events where either the giver_id or receiver_id is IN the array of friends (User.friends), created in the last two weeks.

How can I do this?

IN queries can be done with: where(:giver_id => array)

So I think you can do something like:

scope :party_in, lambda {|friends| where(:giver_id => friends) | where(:receiver_id => friend) }

You can then chain the scope with the rest of your query:

feed = Events.party_in(User.friends).date_gt(Date.today.2.weeks.ago)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM