简体   繁体   English

当前用户的Ruby on Rails范围测试

[英]Ruby on Rails scope test for current user

I have a request table that belongs_to :contact 我有一个belongs_to :contact表的请求表

I want a scope statement in the request.rb file to test if the contact is the current user. 我想要request.rb文件中的作用域语句来测试联系人是否是当前用户。

scope :myopen, where(:contact_id => current_user.id)

Returns: undefined local variable or method `current_user' 返回:未定义的局部变量或方法“ current_user”

What do I use for the current user id? 当前的用户ID应该使用什么?

Thanks 谢谢

You can't do this, nor should you. 您不能这样做,也不应该。 There is no "current" user at the database level. 在数据库级别没有“当前”用户。

You need to pass the user object in to a class-level method: 您需要将用户对象传递给类级方法:

def self.open_for_user(user)
  where(:contact_id => user.id)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM