Note that there are some explanatory texts on larger screens.

plurals
  1. POGet the Next/Previous record in ActiveRecord on Ruby on Rails
    primarykey
    data
    text
    <p>I am trying to get the next/previous record by ActiveRecord. The records should be retrieved <strong>according to the order by 'updated_at'</strong> column.</p> <p>The name of the Model is 'Youtube'. And as the following console, this code couldn't get the right record and I guess the idea of my code seems bad because <strong>updated_at is not always unique</strong> so some records <strong>might have the same time stamp</strong>.</p> <p>How do you get the next/previous record in a right way?</p> <p>Console said below.</p> <pre><code>[57] pry(main)&gt; Youtube.find(1000) Youtube Load (0.5ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 1000]] =&gt; #&lt;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&gt; [58] pry(main)&gt; 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 &gt; '2013-10-27 02:19:56.593969') ORDER BY updated_at DESC LIMIT 1 =&gt; #&lt;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&gt; [59] pry(main)&gt; 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 &lt; '2013-10-27 02:19:56.593969') ORDER BY updated_at DESC LIMIT 1 =&gt; #&lt;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&gt; </code></pre> <p>Youtube has the following default_scope. Although this might be changed according to some situation, but I hope this code would be kept to keep the existing behaviour.</p> <pre><code> default_scope order('updated_at DESC') </code></pre> <p>My trial code for Youtube Model is below.</p> <pre><code> scope :next, lambda{|updated_at| where("updated_at &gt; ?", updated_at).order("updated_at DESC")} scope :previous, lambda {|updated_at| where("updated_at &lt; ?", updated_at).order("updated_at DESC")} </code></pre> <p>...</p> <pre><code> def next self.class.next(updated_at).first end def previous self.class.previous(updated_at).first end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload