【问题标题】:Ruby - How can i get current user in Model?Ruby - 如何在模型中获取当前用户?
【发布时间】:2020-10-29 12:43:11
【问题描述】:

我想列出当前用户 cmets 写的书并等待批准。我试过 where(user_id: current_user.id) 但它不起作用。如何在模型中使用 current_user?

版本:Rails 6

评论模型。

class Comment < ApplicationRecord
  validates :title, presence: true
  validates :content, presence: true

  belongs_to :book
  belongs_to :user

  scope :approved, -> {where(status: true)}
  scope :waiting_for_approval, -> {where(status: false).where(user_id: current_user.id)}
end

用户模型

class User < ApplicationRecord
  before_create :set_username
  
  has_many :books
  has_many :comments
  has_many :offers

  validates_length_of :set_username, 
  :minimum => 5, :maximum => 50, 
  presence: true,
  uniqueness: true
 
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  def set_username
    self.username = self.email.split(/@/).first
  end
end

图书模型

class Book < ApplicationRecord
  validates :title, presence: true
  validates :author, presence: true

  belongs_to :user
  has_many :comments
  has_many :offers

  scope :can_be_listed, -> {where(status: true)}
  scope :can_be_tradable, -> {where(status: true, tradable: true)}
end

【问题讨论】:

标签: ruby-on-rails ruby


【解决方案1】:

与其尝试传递用户 ID,这实际上只是用户模型上的一个简单关系

class Comment < ApplicationRecord
  scope :waiting_for_approval, -> { where(status: false) }
end

class User < ApplicationRecord
  has_many :comments
end

然后您可以在您的控制器或视图中等待当前用户的批准...作为

current_user.comments.waiting_for_approval

【讨论】:

    【解决方案2】:

    使用范围和参数

    scope :waiting_for_approval, -> (user_id) { where(status: false, user_id: user_id) }
    

    【讨论】:

      【解决方案3】:

      在模型中使用current_user 的最简单方法是将其作为参数传递。

      class Comment < ApplicationRecord
        scope :by, ->(user) { where(user_id: user.respond_to?(:id) ? user.id : user) }
        scope :waiting_for_approval, -> { where(status: false) }
      end
      

      然后在您的控制器中执行以下操作:

      comments = Comment.by(current_user).waiting_for_approval
      

      不过我建议尽可能使用answer of SteveTurczyn。

      【讨论】:

        【解决方案4】:

        您必须将current_user 设置为cattr_accessor 的User 类。然后你可以通过调用User.current_user在任何模型中访问它。

        您可以按照以下步骤操作。

        # app/controllers/application_controller.rb
        
        class ApplicationController < ActionController::Base
          before_action :set_current_user
        
          private
        
          def set_current_user
            User.current_user = current_user
          end
        end
        
        # app/models/user.rb
        
        class User < ApplicationRecord
          # your codes ...
        
          cattr_accessor :current_user
        end
        
        # app/models/comment.rb
        
        class Comment < ApplicationRecord
          # your codes ...
        
          scope :waiting_for_approval, -> { where(status: false).where(user_id: User.current_user.id) }
        end
        

        快乐编码:-)

        【讨论】:

        • 如果有多个用户同时访问该应用程序,这将失败。此外,User 类不需要知道任何有关全局或请求级应用程序状态的信息。无论哪个模组删除了我的原始评论,都删除了反对票的背景——没有为什么这种方法不好的书面记录,有人可能会实际使用它并破坏事物。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-06
        • 2020-12-07
        • 2012-04-25
        • 2012-06-15
        • 1970-01-01
        • 2015-03-12
        相关资源
        最近更新 更多