0

创建新记录后,我无法获取记录的 ID。

# /var/www# rails g model Foo name:string
      invoke  active_record
      create    db/migrate/20120904030554_create_foos.rb
      create    app/models/foo.rb
      invoke    test_unit
      create      test/unit/foo_test.rb
      create      test/fixtures/foos.yml
# /var/www# rake db:migrate
==  CreateFoos: migrating =====================================================
-- create_table(:foos)
   -> 0.3451s
==  CreateFoos: migrated (0.3452s) ============================================

# /var/www# rails c
Loading development environment (Rails 3.2.8)
1.9.3p194 :001 > foo = Foo.new(:name => 'bar')
 => #<Foo id: nil, name: "bar", created_at: nil, updated_at: nil> 
1.9.3p194 :002 > foo.save
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `foos` (`created_at`, `name`, `updated_at`) VALUES ('2012-09-04 03:06:26', 'bar', '2012-09-04 03:06:26')
   (103.2ms)  COMMIT
 => true 
1.9.3p194 :004 > Foo.last
  Foo Load (0.5ms)  SELECT `foos`.* FROM `foos` ORDER BY `foos`.`id` DESC LIMIT 1
 => #<Foo id: 1, name: "bar", created_at: "2012-09-04 03:06:26", updated_at: "2012-09-04 03:06:26"> 
1.9.3p194 :003 > foo.inspect
 => "#<Foo id: nil, name: \"bar\", created_at: \"2012-09-04 03:06:26\", updated_at: \"2012-09-04 03:06:26\">" 

这似乎只是我的应用程序中的一个问题。我在一个新的rails new foo应用程序上做了同样的事情,并且 id 出现了。

这怎么可能发生?

4

0 回答 0