简体   繁体   English

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

[英]How do I create a blacklist/whitelist for finding Rails model records?

I want to create a model, "Whitelist" to build a list of users that I do not want displayed in my main model, "User". 我想创建一个模型“ Whitelist”,以建立一个我不想显示在主模型“ User”中的用户列表。

Example Controller 控制器实例

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

Example Model 示例模型

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

I hope this makes sense. 我希望这是有道理的。 Thanks for the help. 谢谢您的帮助。

You could use a named_scope 您可以使用named_scope

So in your user model: 因此,在您的用户模型中:

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

And then in your controller: 然后在您的控制器中:

User.whitelist

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

相关问题 如何创建一个混合了本地记录和远程资源的 Rails 4 模型? - How do I create a Rails 4 model that is a hybrid between local records and remote resources? Rails 4中的黑名单模型 - Blacklist model in Rails 4 Rails 4 强参数:我可以“排除”/黑名单属性而不是许可/白名单吗? - Rails 4 Strong Parameters : can I 'exclude' / blacklist attributes instead of permit / whitelist? Rails-如何在联接模型中更新记录值? - Rails - How do i update a records value in a join model? 如何在Rails中为模型属性创建迁移 - How do I create migrations for model attributes in rails 如何为Rails中的Friends创建关系模型? - How do i create Relational Model for Friends in Rails? 如何在Rails中为模型的哈希属性创建客户序列化程序? - How do I create a customer serializer for a hash attribute of a model in Rails? Rails4建议:如何在单个视图中的模型中创建多个记录? - Rails4 advice: How should I create multiple records in a model from single view? Rails / ActiveRecord:如何按与相关模型的最多关联来排序记录? - Rails/ActiveRecord: How do I order records by the most associations with a related model? 使用Rails验证,如何将一个短语列入白名单,例如“ Learn Ruby” - Using Rails validations, how do I whitelist a phrase, e.g., “Learn Ruby”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM