如何创建黑名单/白名单以查找Rails模型记录?

时间:2010-09-07 18:58:36

标签: ruby-on-rails ruby model filter find

我想创建一个模型“Whitelist”来构建我不希望在我的主模型“User”中显示的用户列表。

示例控制器

def index
    @users = User.find(:all) #These are to be filtered behind the scenes in the model
end

示例模型

class User ActiveRecord::Base
has_many :whitelist
def self.find
    #Add something that will lookup items in the Whitelist model and filter those matches out of a find(:all) in the User model.
end

我希望这是有道理的。谢谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您可以使用named_scope

所以在您的用户模型中:

named_scope :whitelist, :conditions => { :awesome => true }

然后在你的控制器中:

User.whitelist