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)

当你这样做firstall

要不就

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