简体   繁体   English

宝石,更新和兼容性

[英]Gem, update and compatibility

I have a User model, the user can gradually insert information on their profile (age, description, avatar, etc..). 我有一个用户模型,用户可以在其个人资料上逐步插入信息(年龄,描述,头像等。)。 Those users can be viewed in the public web site only if they have complete their entire profile. 只有完整的个人资料,才能在公共网站上查看这些用户。

Whats is the best way in rails to put constraint on query without polluting every single call to Active Record User model. 什么是Rails在查询上施加约束而又不污染对Active Record User模型的每次调用的最佳方法。 Is there're a way for 有没有办法

User.all

to return result with those constraints by default? 在默认情况下返回具有这些约束的结果?

Tks a lot! Tks很多!

You could define a scope. 您可以定义范围。

# user.rb
scope :complete, where("age IS NOT NULL", "description IS NOT NULL",...)

Then you can just do User.complete and it will fetch User objects matching those conditions. 然后,您可以执行User.complete ,它将获取符合这些条件的User对象。 For more information: 欲获得更多信息:

http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html

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

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