我有一个奇怪的场景:我想在P创建者和当前用户成为朋友的时候订购所有帖子P.Rails根据相关模型的属性对条目进行排序
结果将是一个帖子列表,其中包含较新朋友的帖子在顶部和较老朋友帖子的底部。我们可以假设所有用户都是其他用户的朋友,但每个友谊都有不同的创建时间。
我有Post,User和Friendship模型。帖子属于用户,用户有很多友谊。
class User < ActiveRecord::Base
has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => "User"
end
class Post < ActiveRecord::Base
belongs_to :user
end
这样做的最佳方法是什么?谢谢!
你将如何把它转换成一个范围? – MicFin