我有2種型號,用戶和友誼,這裏就是他們的樣子:如何打印我的朋友的帖子和帖子?
class User < ActiveRecord::Base
has_many :posts
has_many :friendships, :conditions => { :confirmed => true }
has_many :friend_requests, :class_name => 'Friendship', :conditions => { :confirmed => false }
has_many :friends, :through => :friendships
has_many :friends_friendships, :through => :friends, :source => :friendships
has_many :friends_of_friends, :through => :friends_friendships, :source => :friend
has_many :friends_listings, :through => :friends, :source => :listings
has_many :friends_posts, :through => :friends, :source => :posts
...
end
和
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => "User"
attr_accessible :friend_id
...
end
,這裏是我如何獲取的當前用戶的帖子記錄帖子他/她的朋友:
@my_posts = current_user.posts.includes(:user)
@friends_posts = current_user.friends_posts.includes(:user)
這很好,但我怎樣才能將我的帖子+朋友的帖子加載到一個變量中並將它們顯示爲在Facebook上?換句話說,我怎樣才能將這兩個查詢合併爲一個?
感謝
我寫了一個SQL來實現這一目標。 –