为什么.first不会在我的模型表中返回“第一个”结果(即ID = 1)?

时间:2013-03-23 10:42:37

标签: ruby-on-rails ruby ruby-on-rails-3

当我执行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"> 

1 个答案:

答案 0 :(得分:2)

按照从磁盘读取的顺序获得结果。它们不是由您的主键订购的。

您可以在question.rb文件中执行此操作

default_scope order(:id)

,当您执行firstall

时,它将按ID排序

或只是

 Question.order(:id).first