0

次のボタンはうまく機能していますが、前のボタンはうまく機能したくありません。面白いことに、SO や他の場所でメソッドを参照するいくつかの異なる方法でこれを構築しようとしましたが、以前は常に最も古い記録に進みます...

私の提出モデルでは:

   def previous
     self.class.first(:conditions => ["created_at < ?", created_at], :order => "created_at asc")
   end

   def next
     self.class.first(:conditions => ["created_at > ?", created_at], :order => "created_at asc")
   end

そして私の見解では:

<% if @submission.previous %>
  <%= link_to "previous", contest_submission_path(@contest, @submission.previous),
                        :class => 'pull-left btn btn-large' %>
<% end %>
<% if @submission.next %>
  <%= link_to "next", contest_submission_path(@contest, @submission.next),
                        :class => 'pull-right btn btn-large' %>
<% end %>

私が言ったように、次はうまくいきますが、前は最も古い記録に戻ります。私はこれで知恵の終わりに近づいています。誰か答えがありますか?

編集済み

ログ:

Started GET "/contests/1/submissions/1" for 127.0.0.1 at 2012-11-30 08:08:52 -0800
Processing by SubmissionsController#show as HTML
  Parameters: {"contest_id"=>"1", "id"=>"1"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY users.created_at DESC LIMIT 1
  Contest Load (0.3ms)  SELECT "contests".* FROM "contests" WHERE "contests"."id" = $1 LIMIT 1  [["id", "1"]]
  Submission Load (0.2ms)  SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 ORDER BY submissions.created_at ASC LIMIT 1  [["id", "1"]]
  CACHE (0.0ms)  SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 ORDER BY submissions.created_at ASC LIMIT 1  [["id", "1"]]
   Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = 1 AND "comments"."commentable_type" = 'Submission' ORDER BY comments.created_at DESC, created_at
   Submission Load (0.3ms)  SELECT "submissions".* FROM "submissions" WHERE (created_at < '2012-10-08 14:32:40.590930') ORDER BY submissions.created_at ASC, created_at asc LIMIT 1
   Submission Load (0.2ms)  SELECT "submissions".* FROM "submissions" WHERE (created_at > '2012-10-08 14:32:40.590930') ORDER BY submissions.created_at ASC, created_at asc LIMIT 1
  CACHE (0.0ms)  SELECT "submissions".* FROM "submissions" WHERE (created_at > '2012-10-08 14:32:40.590930') ORDER BY submissions.created_at ASC, created_at asc LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY users.created_at DESC LIMIT 1
  Image Load (0.2ms)  SELECT "images".* FROM "images" WHERE "images"."parent_id" = 2 AND "images"."parent_type" = 'User' LIMIT 1
  Rendered submissions/_follow_unfollow.html.erb (0.1ms)
  Image Load (0.4ms)  SELECT "images".* FROM "images" WHERE "images"."parent_id" = 1 AND "images"."parent_type" = 'Submission' LIMIT 1
  Rendered submissions/_hide_comments_form.html.erb (1.9ms)
  Rendered comments/_form.html.erb (2.5ms)
  Rendered comments/_comment.html.erb (3.5ms)
  Rendered submissions/show.html.erb within layouts/application (21.7ms)
  Rendered layouts/_stylesheets.html.erb (4.3ms)
  CACHE (0.0ms)  SELECT "images".* FROM "images" WHERE "images"."parent_id" = 2 AND "images"."parent_type" = 'User' LIMIT 1
  Role Load (0.5ms)  SELECT "roles".* FROM "roles" INNER JOIN "assignments" ON "roles"."id" = "assignments"."role_id" WHERE "assignments"."user_id" = 2
  Rendered layouts/_header.html.erb (4.6ms)
  Rendered layouts/_footer.html.erb (0.7ms)

105 ミリ秒で 200 OK を完了 (ビュー: 97.8 ミリ秒 | ActiveRecord: 3.0 ミリ秒)

ログ #2

Started GET "/contests/1/submissions/%23%3CActiveRecord::Relation:0x007fc3b5e50af8%3E" for 127.0.0.1 at 2012-11-30 08:43:15 -0800
Processing by SubmissionsController#show as HTML
  Parameters: {"contest_id"=>"1", "id"=>"#<ActiveRecord::Relation:0x007fc3b5e50af8>"}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY users.created_at DESC LIMIT 1
  Contest Load (0.3ms)  SELECT "contests".* FROM "contests" WHERE "contests"."id" = $1 LIMIT 1  [["id", "1"]]
  Submission Load (0.3ms)  SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 LIMIT 1  [["id", "#<ActiveRecord::Relation:0x007fc3b5e50af8>"]]
Completed 500 Internal Server Error in 4ms

ActiveRecord::RecordNotFound (Couldn't find Submission with id=#   <ActiveRecord::Relation:0x007fc3b5e50af8>):
app/controllers/submissions_controller.rb:89:in `find_submission'

最終的解決:

submit.rb で

   default_scope order: 'submissions.created_at DESC'

   def previous_sub
     self.class.where("created_at > ?", created_at).reorder("created_at asc").first 
   end

   def next_sub
     self.class.where("created_at < ?", created_at).reorder("created_at desc").first 
   end

ビュー (表示):

<% if @submission.previous_sub %>
  <%= link_to "previous", contest_submission_path(@contest, @submission.previous_sub),
                        :class => 'pull-left btn btn-large' %>
<% end %>
<% if @submission.next_sub %>
  <%= link_to "next", contest_submission_path(@contest, @submission.next_sub),
                        :class => 'pull-right btn btn-large' %>
<% end %>

メソッドの名前を交換することになりました。これにより、ショー レコードの順序がインデックスと一致するようになりました。インデックスは、DESC 順または最新のものから最も古いものへとスコープが設定されます。

4

1 に答える 1

0

コードは正しいですが、ログが表示されます

SELECT "submissions".* FROM "submissions" 
WHERE (created_at < '2012-10-08 14:32:40.590930') 
ORDER BY submissions.created_at ASC, created_at asc LIMIT 1

これが以前を検索するクエリである場合、ここで失敗します (以前の日付を昇順で並べ替え、最初の日付を取得するため)。変更を加えた後にアプリを再起動するのを忘れた可能性があります (ただし、開発モードでは自動的に再読み込みされるはずです)。

default_scopeまたは、条件を破る他のスコープがある可能性があります。なぜあるのだろうか-2submissions.created_at ASC, created_at asc回?おそらくsubmissions.created_at ASCデフォルトのスコープです。

reorder新しい AR クエリ構文を使用し、おそらく他の順序付けスコープを除外することをお勧めします。

self.class.where("created_at < ?", created_at).reorder("created_at desc").first 
于 2012-11-30T16:23:43.790 に答える