I have a simple rails app where users can give virtual gifts to each other. Each gift belongs to two users, the giver and the receiver.
In my User.rb>
I would create a custom method in my Gift model, try this:
gift.rb
class Gift < ActiveResource::Base
...
def self.all_gifts(user_id)
follower_ids = User.find(user_id).following_users.pluck(:id)
Gift.where('giver_id in (?) OR receiver_id in (?)', follower_ids, follower_ids).uniq
end
...
This will end up generating and running a SQL call that looks like this:
SELECT DISTINCT "gifts".* FROM "gifts" WHERE (giver_id in (1,2,3) OR receiver_id in (1,2,3))
Given that users with IDs 1, 2, and 3 follow the user. You should be able to paginate these results like this in your controller:
id = current_user.id
Gift.all_gifts(id).paginate # any paginate attributes