0

我正在尝试计算所有姓氏为空的用户。

我正在尝试这些,但它们不起作用

User.where("last_name = ?", "").count
User.where("last_name = ?", nil).count

调用 all 反对 nil 有效,但我仍然需要选择那些 last_name 为空的人。

User.where("last_name != ?", nil).count
4

2 回答 2

6
User.where(:last_name => nil).count

或使用 ARel:

User.where(User.arel_table[:last_name] == nil).count
于 2012-05-27T09:10:14.310 回答
1

对于 mysql 可能:

User.where("last_name is null").count

否则可能:

User.find_all_by_last_name(nil).count
于 2012-05-27T09:07:46.617 回答