【发布时间】:2013-11-06 17:25:21
【问题描述】:
我正在尝试通过 ActiveRecord 获取下一条/上一条记录。记录应按照'updated_at'列的顺序检索。
模型的名称是“Youtube”。而作为下面的控制台,这段代码无法获得正确的记录,我猜我的代码的想法似乎很糟糕,因为 updated_at 并不总是唯一的,所以一些记录可能具有相同的时间戳。
你如何以正确的方式获得下一个/上一个记录?
控制台如下所述。
[57] pry(main)> Youtube.find(1000)
Youtube Load (0.5ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 1000]]
=> #<Youtube id: 1000, author_id: 2, category_label: nil, generated_by: 1, title: "Is Kenya Mall Shooting Over? Were Americans Among A...", video_id: "4T1szQIQcNI", created_at: "2013-09-30 18:31:21", updated_at: "2013-10-27 02:19:56", subtitles: nil>
[58] pry(main)> Youtube.find(1000).next
Youtube Load (0.6ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 1000]]
Sun, 27 Oct 2013 02:19:56 UTC +00:00
Youtube Load (256.6ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at > '2013-10-27 02:19:56.593969') ORDER BY updated_at DESC LIMIT 1
=> #<Youtube id: 67003, author_id: 75, category_label: nil, generated_by: 1, title: "Jewelry Photography : Lenses for Jewelry Photograph...", video_id: "NqA7OZL4tzw", created_at: "2013-10-09 17:18:53", updated_at: "2013-10-28 02:17:33", subtitles: nil>
[59] pry(main)> Youtube.find(1000).previous
Youtube Load (0.6ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 1000]]
Sun, 27 Oct 2013 02:19:56 UTC +00:00
Youtube Load (56.3ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at < '2013-10-27 02:19:56.593969') ORDER BY updated_at DESC LIMIT 1
=> #<Youtube id: 999, author_id: 8, category_label: nil, generated_by: 1, title: "Authors@Google: Richard Moore, Ned Boulting, and Da...", video_id: "4SCzfuJAyJw", created_at: "2013-09-30 18:31:21", updated_at: "2013-10-27 02:19:55", subtitles: nil>
Youtube 有以下 default_scope。虽然这可能会根据某些情况进行更改,但我希望保留此代码以保持现有行为。
default_scope order('updated_at DESC')
我的 Youtube 模型试用代码如下。
scope :next, lambda{|updated_at| where("updated_at > ?",
updated_at).order("updated_at DESC")}
scope :previous, lambda {|updated_at| where("updated_at < ?",
updated_at).order("updated_at DESC")}
...
def next
self.class.next(updated_at).first
end
def previous
self.class.previous(updated_at).first
end
【问题讨论】:
-
Damien,我检查了stackoverflow.com/questions/7394088/…,但如上所述,这不是本案的正确解决方案。
标签: ruby-on-rails ruby activerecord ruby-on-rails-3.2 rails-activerecord