Retrieving posts of all users that a user follows - rails - ActiveRecord

前端 未结 2 1372
夕颜
夕颜 2021-01-24 13:54

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

2条回答
  •  醉话见心
    2021-01-24 14:30

    Tested this on Rails 4 and associations(not the gem). This will give distinct gift records ordered by created_at :-

    @gifts = User.find(id).following_users.joins(:received_gifts, :given_gifts).select('DISTINCT gifts. *')

    Also as per the documentation, I think you should use followers_scoped instead of followers inorder to get an Arel scope instead of an array of elements

提交回复
热议问题