gpt4 book ai didi

mysql - 在 Rails 模型中使用范围收集 "private"帖子

转载 作者:行者123 更新时间:2023-11-29 04:45:23 24 4
gpt4 key购买 nike

我有一个用户帖子表,其中一些是私有(private)的,由表中的 bool 列(隐私)表示(true 是私有(private)的)。在我的 livefeed View (posts/index.html.erb) 中,我只想显示所有用户的非私有(private)帖子。我可以通过我的示波器执行此操作吗?

注意:在我的用户源 View 中,我显示了当前用户的私有(private)和非私有(private)帖子。

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

posts/index.html.erb

<%= render @posts %>

users/show.html.erb

<%= render @posts %>

最佳答案

Rails >=4.1 不再允许名称匹配 BLACKLISTED_CLASS_METHODS 的作用域(即 publicprivateprotectedallocatenewnameparentsuperclass ...参见:GitHub 上的 BLACKLISTED_CLASS_METHODSdangerous_class_method?) .所以,...

...虽然这样:

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

...适用于 Rails <4.1,您可能想尝试类似的东西

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

...以确保 future 的兼容性。

关于mysql - 在 Rails 模型中使用范围收集 "private"帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com