1

私が行うQuestion.firstと、レコードが返されますが、id=7....ID=1私のデータベースにはがあり、それは返されません。

 > Question.first
  Question Load (1.8ms)  SELECT "questions".* FROM "questions" LIMIT 1
 => #<Question id: 7, so_id: 1642028, creation_date: "2009-10-29 06:57:45", score: 1812, accepted_answer_so_id: 1642035, title: "What is the name of this operator: &quot;--&gt;&quo...", view_count: 124958, link: "http://stackoverflow.com/questions/1642028/what-is-...", body: "<p>After reading \"<a href=\"http://groups.google.com...", answer_count: 17, is_answered: true, owner: "GManNickG", created_at: "2013-03-23 07:02:48", updated_at: "2013-03-23 07:02:48", accepted_answer_id: nil> 
1.9.3p392 :026 > Question.find(1)
  Question Load (1.9ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT 1  [["id", 1]]
 => #<Question id: 1, so_id: 11227809, creation_date: "2012-06-27 13:51:36", score: 4072, accepted_answer_so_id: 11227902, title: "Why is processing a sorted array faster than an uns...", view_count: 216266, link: "http://stackoverflow.com/questions/11227809/why-is-...", body: "<p>Here is a piece of C++ code that shows some very...", answer_count: 9, is_answered: true, owner: "GManNickG", created_at: "2013-03-23 07:02:10", updated_at: "2013-03-23 10:27:17", accepted_answer_id: nil> 

どちらも異なる質問ですが、本当の問題は、なぜ?.firstでレコードを返さないのかということです。id=1

編集 1

Questionこれは私のテーブルのスキーマです:

  create_table "questions", :force => true do |t|
    t.integer  "so_id"
    t.datetime "creation_date"
    t.integer  "score"
    t.integer  "accepted_answer_so_id"
    t.string   "title"
    t.integer  "view_count"
    t.string   "link"
    t.text     "body"
    t.integer  "answer_count"
    t.boolean  "is_answered"
    t.string   "owner"
    t.datetime "created_at",            :null => false
    t.datetime "updated_at",            :null => false
    t.integer  "accepted_answer_id"
  end

  add_index "questions", ["accepted_answer_so_id"], :name => "index_questions_on_accepted_answer_so_id"
  add_index "questions", ["so_id"], :name => "index_questions_on_so_id"
  add_index "questions", ["title"], :name => "index_questions_on_title"

編集 2

それが別のモデルでも発生することを確認しましAnswerた(ここで何かファンキーなことが起こっています):

> Answer.first
  Answer Load (1.0ms)  SELECT "answers".* FROM "answers" LIMIT 1
 => #<Answer id: 6629, so_id: 1048093, creation_date: "2009-06-26 08:58:19", is_accepted: false, question_id: 400, owner: "Christian Hayter", score: 10, created_at: "2013-03-23 09:21:33", updated_at: "2013-03-23 09:23:32", body: "<p>I'd prefer <code>INFORMATION_SCHEMA.COLUMNS</cod..."> 
1.9.3p392 :008 > Answer.find(1)
  Answer Load (44.7ms)  SELECT "answers".* FROM "answers" WHERE "answers"."id" = $1 LIMIT 1  [["id", 1]]
 => #<Answer id: 1, so_id: 11227902, creation_date: "2012-06-27 13:56:42", is_accepted: true, question_id: 1, owner: "Mysticial", score: 6423, created_at: "2013-03-23 07:02:10", updated_at: "2013-03-23 09:43:12", body: "<p><strong>You are the victim of <a href=\"http://en..."> 

ただし、別のモデルではそうしません。

> Tag.first
  Tag Load (24.5ms)  SELECT "tags".* FROM "tags" LIMIT 1
 => #<Tag id: 1, name: "java", num_questions: 388310, created_at: "2013-03-23 07:02:09", updated_at: "2013-03-23 07:02:09"> 
4

1 に答える 1

2

ディスクから読み取られた順序で結果を取得します。それらは主キー順に並べられていません。

あなたはあなたのquestion.rbファイルでこれを行うことができます

default_scope order(:id)

あなたがするとき、それはIDで注文しますfirstまたはall

あるいは単に

 Question.order(:id).first
于 2013-03-23T10:50:01.753 に答える