【发布时间】:2014-03-05 01:54:16
【问题描述】:
我使用 Railstutorials 创建关注者和 follow_users http://ruby.railstutorial.org/chapters/following-users#top
在我想显示特定人员的关注者/followed_users 的页面上,我想根据关系的创建时间来显示他们。
@users = @user.followers.order("created_at DESC")
类似 ^^ 的内容只显示创建用户的时间,而不是创建关系的时间。如何有效地运行此查询以获得正确的排序?
def following
@users = @user.followed_users
end
def followers
@users = @user.followers
end
-User Model-
has_many :relationships, foreign_key: "follower_id", :dependent => :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
- Relationship Model -
belongs_to :follower, class_name: "User", touch: true
belongs_to :followed, class_name: "User", touch: true
validates :follower_id, presence: true
validates :followed_id, presence: true
【问题讨论】:
-
我认为这是不可能的,除非你在
relationships表上有一个created_at,在这种情况下我会回答这个问题 -
我有一个 created_at 用于关系
标签: sql ruby-on-rails ruby-on-rails-4