Mongoid order_by 布尔值

发布于 2024-12-18 04:18:05 字数 372 浏览 2 评论 0原文

我的数据库中有一个通知表,其中包含属性:timestamp:datetimeread:boolean。我想要的是查询和订购我的通知;首先是它们是否被读取,然后是它们获得的时间戳,然后将通知数量限制为 10。

我尝试过的查询看起来像这样:

@user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10)

这只会给我一个错误,我将其范围缩小到证明布尔字段是罪魁祸首。

是否存在按真/假值排序的现有方法,或者我应该诉诸使用某种自定义字段序列化将 True 和 False 转换为 1 和 0?

I have a notifications table in my database which contains the attributes: timestamp:datetime and read:boolean. What I want is to query and order my notifications; first by if they are read or not, then after which timestamp they've got, then limit the amount of notifications to 10.

The query I've tried looks something like this:

@user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10)

Which only gives me an error which I narrowed down to proving the boolean field as the culprit.

Is there an existing way of ordering by true/false values or should I resort to using some kind of Custom Field Serialization transforming True's and False's to 1's and 0's?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌酱 2024-12-25 04:18:05

您指定的内容适用于 Mongoid 2.4.10、mongo 1.3、rails 3.2.3。希望以下内容能够帮助解决您的问题。

class User
  include Mongoid::Document

  field :name, type: String
  has_many :notifications
end

class Notification
  include Mongoid::Document

  field :read, type: Boolean
  field :timestamp, type: DateTime
  belongs_to :user
end

test/unit/notification_test.rb

require 'test_helper'

class NotificationTest < ActiveSupport::TestCase
  def setup
    User.delete_all
    Notification.delete_all
  end

  test "order_by boolean" do
    @user = User.create(name: 'Gary')
    [
      [true, 1.day.ago], [false, 2.days.ago], [false, 3.days.ago], [true, 5.days.ago], [false, 8.days.ago], [true, 11.days.ago],
      [false, 4.days.ago], [true, 6.days.ago], [true, 7.days.ago], [false, 9.days.ago], [false, 10.days.ago]
    ].each do |read, timestamp|
      @user.notifications << Notification.create(read: read, timestamp: timestamp)
    end
    assert_equal(1, User.count)
    assert_equal(11, Notification.count)
    result = @user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10).to_a
    assert_equal(10, result.size)
    result.each do |r|
      p [r.read, r.timestamp]
    end
  end
end

测试输出

Run options: --name=test_order_by_boolean

# Running tests:

[true, Mon, 28 May 2012 12:33:49 -0400]
[true, Thu, 24 May 2012 12:33:49 -0400]
[true, Wed, 23 May 2012 12:33:49 -0400]
[true, Tue, 22 May 2012 12:33:49 -0400]
[true, Fri, 18 May 2012 12:33:49 -0400]
[false, Sun, 27 May 2012 12:33:49 -0400]
[false, Sat, 26 May 2012 12:33:49 -0400]
[false, Fri, 25 May 2012 12:33:49 -0400]
[false, Mon, 21 May 2012 12:33:49 -0400]
[false, Sun, 20 May 2012 12:33:49 -0400]
.

Finished tests in 0.023062s, 43.3614 tests/s, 130.0841 assertions/s.

1 tests, 3 assertions, 0 failures, 0 errors, 0 skips

What you specify works for me with Mongoid 2.4.10, mongo 1.3, rails 3.2.3. Hope that the following helps to address your problem.

class User
  include Mongoid::Document

  field :name, type: String
  has_many :notifications
end

class Notification
  include Mongoid::Document

  field :read, type: Boolean
  field :timestamp, type: DateTime
  belongs_to :user
end

test/unit/notification_test.rb

require 'test_helper'

class NotificationTest < ActiveSupport::TestCase
  def setup
    User.delete_all
    Notification.delete_all
  end

  test "order_by boolean" do
    @user = User.create(name: 'Gary')
    [
      [true, 1.day.ago], [false, 2.days.ago], [false, 3.days.ago], [true, 5.days.ago], [false, 8.days.ago], [true, 11.days.ago],
      [false, 4.days.ago], [true, 6.days.ago], [true, 7.days.ago], [false, 9.days.ago], [false, 10.days.ago]
    ].each do |read, timestamp|
      @user.notifications << Notification.create(read: read, timestamp: timestamp)
    end
    assert_equal(1, User.count)
    assert_equal(11, Notification.count)
    result = @user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10).to_a
    assert_equal(10, result.size)
    result.each do |r|
      p [r.read, r.timestamp]
    end
  end
end

test output

Run options: --name=test_order_by_boolean

# Running tests:

[true, Mon, 28 May 2012 12:33:49 -0400]
[true, Thu, 24 May 2012 12:33:49 -0400]
[true, Wed, 23 May 2012 12:33:49 -0400]
[true, Tue, 22 May 2012 12:33:49 -0400]
[true, Fri, 18 May 2012 12:33:49 -0400]
[false, Sun, 27 May 2012 12:33:49 -0400]
[false, Sat, 26 May 2012 12:33:49 -0400]
[false, Fri, 25 May 2012 12:33:49 -0400]
[false, Mon, 21 May 2012 12:33:49 -0400]
[false, Sun, 20 May 2012 12:33:49 -0400]
.

Finished tests in 0.023062s, 43.3614 tests/s, 130.0841 assertions/s.

1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文