Skip to content Skip to sidebar Skip to footer

Order Users Based On When The Relationship Was Created_at

I used the Railstutorials to create followers and followed_users http://ruby.railstutorial.org/chapters/following-users#top On the page where I want to show a specific persons' fol

Solution 1:

Since your user has there two relationships, you can easily access that table with the direction you want.

has_many :relationships, foreign_key:"follower_id", dependent::destroy
has_many :reverse_relationships, foreign_key:"followed_id"

First Answer (when they're a follower)

You have to use the relationships table because that record gets created when you get a new follower, thus you do this:

@user.relationships.order("created_at DESC").collect { |r| User.find(r.followed) }

Second Answer (when they're followed)

@user.reverse_relationships.order("created_at DESC").collect { |r| User.find(r.follower) }

Post a Comment for "Order Users Based On When The Relationship Was Created_at"