1

ユーザー投稿のテーブルがあり、その一部は非公開であり、テーブル内のブール列 (プライバシー) で示されます (非公開の場合は true)。私の livefeed ビュー (posts/index.html.erb) では、すべてのユーザーの非公開の投稿のみを表示したいと考えています。スコープを介してこれを行うことはできますか?

注: 私の usersfeed ビューでは、current_user の非公開および非公開の投稿を表示しています。

Post.rb

class Post < ActiveRecord::Base
  belongs_to :user
  # the top scope is sorting by "featured" posts (a boolean column in the Posts table)
  scope :livefeed_order, order('featured DESC, created_at DESC').limit(40)
  scope :userfeed_order, order('created_at DESC')
end

posts_controller.rb

class PostsController < ApplicationController
  before_filter :signed_in_user, except: [:show]

  def index #Livefeed
    @posts = Post.livefeed_order
  end
end

users_controller.rb

class UsersController < ApplicationController
  before_filter :signed_in_user, only: [:edit, :update, :show]

  def show
    @user = User.find(params[:id])
    @posts = @user.posts.userfeed_order
  end
end

投稿/index.html.erb

<%= render @posts %>

ユーザー/show.html.erb

<%= render @posts %>
4

2 に答える 2

3

Rails >=4.1 では、 BLACKLISTED_CLASS_METHODS (IE publicprivateprotectedallocatenew、...参照: GitHubのBLACKLISTED_CLASS_METHODSおよび危険な_クラス_メソッド? )に一致する名前を持つスコープが許可されなくなりました。そう、 ...nameparentsuperclass

...これは:

scope :public, -> { where(privacy: false) }

...Rails <4.1では問題ありません。次のようなものを試してみてください

scope :only_public, -> { where(privacy: false) }
scope :only_private, -> { where(privacy: true) }

...将来の互換性を確保するため。

于 2015-07-31T00:53:46.283 に答える