Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I did try and error and found below is one of the solutions.</p> <p>code is here.</p> <pre><code> def next self.class.unscoped.where("updated_at &lt;= ? AND id != ?", updated_at, id).order("updated_at DESC").first end def previous self.class.unscoped.where("updated_at &gt;= ? AND id != ?", updated_at, id).order("updated_at ASC").first end </code></pre> <p>test is here.</p> <pre><code>[210] pry(main)&gt; Youtube.find(100) Youtube Load (0.8ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]] =&gt; #&lt;Youtube id: 100, author_id: 5, category_label: nil, generated_by: 1, title: "Woman's Profane Dunkin Donuts Rant Goes Viral", video_id: "-aqN7KdWgQE", created_at: "2013-09-30 18:19:42", updated_at: "2013-10-27 00:47:37", subtitles: nil&gt; [211] pry(main)&gt; Youtube.find(100).next Youtube Load (0.7ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]] Youtube Load (95.9ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at &lt;= '2013-10-27 00:47:37.241076' AND id != 100) ORDER BY updated_at DESC LIMIT 1 =&gt; #&lt;Youtube id: 99, author_id: 6, category_label: nil, generated_by: 1, title: "Editing physical locations in Google Maps", video_id: "-amPC4fcY0U", created_at: "2013-09-30 18:19:42", updated_at: "2013-10-27 00:47:36", subtitles: nil&gt; [212] pry(main)&gt; Youtube.find(100).next.previous Youtube Load (0.7ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]] Youtube Load (68.8ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at &lt;= '2013-10-27 00:47:37.241076' AND id != 100) ORDER BY updated_at DESC LIMIT 1 Youtube Load (79.5ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at &gt;= '2013-10-27 00:47:36.162671' AND id != 99) ORDER BY updated_at ASC LIMIT 1 =&gt; #&lt;Youtube id: 100, author_id: 5, category_label: nil, generated_by: 1, title: "Woman's Profane Dunkin Donuts Rant Goes Viral", video_id: "-aqN7KdWgQE", created_at: "2013-09-30 18:19:42", updated_at: "2013-10-27 00:47:37", subtitles: nil&gt; [213] pry(main)&gt; Youtube.find(100) === Youtube.find(100).next.previous Youtube Load (0.8ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]] Youtube Load (4.8ms) SELECT "youtubes".* FROM "youtubes" WHERE "youtubes"."id" = $1 ORDER BY updated_at DESC LIMIT 1 [["id", 100]] Youtube Load (99.7ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at &lt;= '2013-10-27 00:47:37.241076' AND id != 100) ORDER BY updated_at DESC LIMIT 1 Youtube Load (79.6ms) SELECT "youtubes".* FROM "youtubes" WHERE (updated_at &gt;= '2013-10-27 00:47:36.162671' AND id != 99) ORDER BY updated_at ASC LIMIT 1 =&gt; true </code></pre>
 

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