如何显示current_user的朋友帖子以及一条记录中的热门内容?

时间:2016-06-03 11:03:14

标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-3.2 scopes

我已经设置了我的范围并且所有工作都按预期工作但我可以找到一种方法来过滤一个范围内的朋友帖子。 在模型中

scope :popular, ->{ where("cached_votes_up > '20'") } # change this to a much bigger number overtime
scope :following, ->{ where(Post.current_user.following.ids.include? user_id) }
scope :ordered, -> { order(created_at: :desc) }

在控制器中

@post = Post.ordered.popular.following.paginate(:per_page => 10, :page => 1)

如何为作用域添加以下/好友过滤,以便我可以将它们与其他作用域合并。

我正试图让页面显示超过20个喜欢的帖子,并且即使他们不超过20,也会显示朋友的帖子。

提前致谢。

编辑:

class Post < ActiveRecord::Base
include PublicActivity::Common
cattr_accessor :current_usercontroller.current_user}
scope :popular, ->{ where("cached_votes_up > '1'") } # change this to a much bigger number overtimeuser_id) }
scope :ordered, -> { order(created_at: :desc) }
acts_as_votable
has_attached_file :image, styles: { medium: "550", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
belongs_to :user
belongs_to :category
validates :user_id, presence: true
validates :image, presence: true
default_scope { order("created_at DESC")}
has_many :comments
is_impressionable # :counter_cache => true, :column_name => :my_column_name, :unique => true
  def previous_post
    self.class.where("created_at < ?", created_at).order(created_at: :desc).first
  end

  def next_post
    self.class.where("created_at > ?", created_at).order(created_at: :asc).last
  end

和relationships.rb

class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true

和user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  include PublicActivity::Common
  acts_as_voter
  is_impressionable
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "missing.jpg"
  has_attached_file :cover, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "missing.jpg"
  has_many :posts, dependent: :destroy

  has_many :active_relationships, class_name: 'Relationship', foreign_key: "follower_id", dependent: :destroy
  has_many :passive_relationships, class_name: 'Relationship', foreign_key: "followed_id", dependent: :destroy

  has_many :following, through: :active_relationships, source: :followed
  has_many :followers, through: :passive_relationships, source: :follower

  has_many :comments
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates_attachment :image, :cover,
                     content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }

  def follow(other)
    active_relationships.create(followed_id: other.id)
  end

  def unfollow(other)
    active_relationships.find_by(followed_id: other.id).destroy
  end

  def following?(other)
    following.include?(other)
  end  

end

0 个答案:

没有答案