首页 文章

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

提问于
浏览
1

我想创建一个模型,“白名单”来构建我不希望在我的主模型“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 回答

  • 3

    你可以使用named_scope

    所以在你的用户模型中:

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

    然后在你的控制器中:

    User.whitelist
    

相关问题