我有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来实现这一目标。 –