我有一個奇怪的場景:我想在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