Ruby-on-rails 查找存在属性的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17742623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:20:52  来源:igfitidea点击:

Find records where an attribute is present

ruby-on-railsruby

提问by Fellow Stranger

I have a Usermodel with the attributes username, emailand name.

我有一个User具有属性的模型username,emailname.

usernameand emailare required upon signup, but not name.

username并且email在注册时是必需的,但不是name

What would be the query to find all users that have filled out name(i.e. it is no nil)?

查找所有已填写的用户name(即不为零)的查询是什么?

The query should be at least Rails 3.2 and 4.0 compatible.

查询至少应与 Rails 3.2 和 4.0 兼容。

I'm thinking something in the lines of:

我在想以下几点:

User.where(name: present?)

采纳答案by fiedl

present?

展示?

present?is essentially not nil and not empty?:

present?本质上是not nil and not empty?

class Object
  def present?
    !blank?
  end
  def blank?
    respond_to?(:empty?) ? !!empty? : !self
  end
end

ActiveRecord condition

活动记录条件

In Rails 4, not conditionscan be done without raw sql code.

在 Rails 4 中,没有原始 sql 代码就无法完成条件

# Both lines lead to the same result:
User.where.not(name: [nil, ""])
User.where.not(name: nil).where.not(name: "")

Since there is no raw sql code, you don't have to worry about if they work with every database adapter. In fact, this works fine for both, mysql and postgres.

由于没有原始 sql 代码,您不必担心它们是否适用于每个数据库适配器。事实上,这对 mysql 和 postgres 都适用。

to_sql

to_sql

You can see how they translate to sql queries if you append .to_sql, for example in the rails console.

如果您 append .to_sql,您可以看到它们如何转换为 sql 查询,例如在 rails 控制台中。

# rails console

User.where.not(name: [nil, ""]).to_sql
# => "SELECT \"users\".* FROM \"users\" WHERE (NOT ((\"users\".\"name\" = '' OR \"users\".\"name\" IS NULL)))"

User.where.not(name: nil).where.not(name: "").to_sql
# => "SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"name\" IS NOT NULL) AND (\"users\".\"name\" != '')"

Further Reading

进一步阅读

回答by nathanvda

An empty value in a database gets a special value NULL. Testing whether is set uses the special comparator IS NULLor IS NOT NULL.

数据库中的空值获得一个特殊值NULL。测试是否设置使用特殊比较器IS NULLIS NOT NULL

Then there still remains the possibility that an empty string was filled in, so a complete test would be

然后仍然存在填充空字符串的可能性,因此完整的测试将是

@users = User.where("name is NOT NULL and name != ''")

[UPDATED for rails 4+]

[已更新 Rails 4+]

Since rails 4 we can write:

从 rails 4 开始,我们可以这样写:

User.where.not(name: [nil, ""])

which will generate the same query. Awesome :)

这将生成相同的查询。惊人的 :)

回答by gstraehle

NOT SQL queries can be built by where.not

可以通过 where.not 构建 NOT SQL 查询

@users = User.where.not(name: nil)

回答by Nobita

Try this:

尝试这个:

User.where("name IS NOT NULL AND name != ?", "")

I edited my answer as per @nathavanda comments, which his answer in my opinion should be the accepted one.

我根据@nathavanda 的评论编辑了我的答案,在我看来,他的答案应该是公认的答案。