- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的数据库中有一个通知表,其中包含以下属性:timestamp:datetime
和 read:boolean
.我想要的是查询和订购我的通知;首先是它们是否被读取,然后是它们获得的时间戳,然后将通知数量限制为 10。
我试过的查询看起来像这样:
@user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10)
最佳答案
您指定的内容适用于 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
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
关于ruby-on-rails-3.1 - Mongoid order_by bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8295857/
如何根据多对多关系条件是否为真来对查询集进行排序? 即 class Publication(models.Model): name = models.CharField(max_length=
我想获取 MyModel order_by('-end_date') 的最后 100 条记录,并对不同的获胜者类型进行 SUM 注释 MyModel.objects.all()[:100].order
我想根据两个因素对 QuerySet 进行排序。 A 和 B,但它应该将 A 优先于 B。也就是说,它应该对 A 进行排序,但是如果 A 对 QuerySet 中的两个对象相等,则它应该对因子 B 对
重复: Using a Django custom model method property in order_by() 我有两个模型;一个存储帖子,另一个存储对这些帖子进行的投票,使用外键字段进行
我在模型 roll_numb 中有字段。 roll_numb 的值如下。 070-001 070-007 070-343 080-002 080-008 当我 order_by roll_numb 时
所以我在使用order_by函数的查询中使用preload遇到麻烦。 通常我在order_by函数中使用list_member list_member def list_members() do qu
我们有一个复杂的查询,它使用跨表和 order_by 的联接。 示例如下: select distinct `accounts`.`id`, `accounts`.`number_of_lis
我有一个这样的数据库 order_id | date | qty ------------------------------------ a |2018-1
假设我有一列包含 1-100 之间的浮点值。 我希望能够将该值转换为 1-10 之间精度较低的整数,然后根据这个新值对结果进行排序。 想要降低排序的精确度似乎很奇怪,但 SQL 语句按 2 列排序,如
我有范围内的距离数据,例如 5-10、0-5、10-15、 我正在使用此查询来按排序顺序获取数据: $this->db->select("distance_from_mall"); $this->db
我必须对包含主机名的对象列表进行排序。 主机名采用以下格式:h1、h5、h10、h12、h12-abc、h1000、x10 如果我使用 order_by('hostname') 它会这样排序: h1,
我在 MySQL 数据库中有一系列“编号”的 varchar 字段: +-----------+ | name | +-----------+ | thing 1 | | thing
我不想运行查询来获取所有超过 6 个 com 的文章,然后根据 com 列表的长度进行排序,为此,我这样做了: ArticleModel.objects.filter(com__6__exists=
我有一个模型 Line,其中包含一个 total_value 和一个 group 字段。我使用以下代码获取给定组中具有最高值的 10 行: group_lines = Line.objects.fil
已阅读 django docs on order_by 有一个注释/警告(如果我理解正确的话)说: 如果您使用多值字段对查询集进行排序,则该查询集中具有多个相关项的每个元素都将多次添加到由 order
我正在使用 Django 1.7.7。 我想知道是否有人经历过这种情况。这是我的查询: events = Event.objects.filter( Q(date__gt=my_date) |
我在我的 template.php 文件中使用查询来显示 wooCommerce按类别分类的产品。 我的查询如下所示: 'product', 'stock' => 1, 'pos
我处于必须通过用于存储街道地址的 CharField 输出相当大的对象列表的情况。 我的问题是,显然数据是按 ASCII 代码排序的,因为它是一个 Charfield,具有可预测的结果......它像
Foo.objects.all().distinct('foo')不会删除重复项 Foo.objects.all().order_by().distinct('foo')确实删除它们。 我有 Meta
我喜欢 Django,喜欢它实现的分页功能。但是,我在尝试跨多个页面拆分随机排序的查询集时遇到了问题。 例如,我在查询集中有 100 个元素,并希望一次显示 25 个。将上下文对象作为随机排序的查询集
我是一名优秀的程序员,十分优秀!