Howoto get id of new record after model.save

Posted by tonymarschall on Stack Overflow See other posts from Stack Overflow or by tonymarschall
Published on 2012-09-03T08:11:06Z Indexed on 2012/09/04 15:39 UTC
Read the original article Hit count: 200

Filed under:
|
|

I have a model with the following db structure:

mysql> describe units;
+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| id         | int(11)  | NO   | PRI | NULL    | auto_increment |
| name         | varchar(128) | NO   |     | NULL    |                |
| created_at | datetime | NO   |     | NULL    |                |
| updated_at | datetime | NO   |     | NULL    |                |
+------------+----------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

After creating a new record an saving i can not get the id of the record.

1.9.3p194 :001 > unit = Unit.new(:name => 'test')
 => #<Unit id: nil, name: "test", created_at: nil, updated_at: nil> 
1.9.3p194 :002 > unit.save
   (0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `units` (`created_at`, `name`, `updated_at`) VALUES ('2012-08-31 23:48:12', 'test', '2012-08-31 23:48:12')
   (144.6ms)  COMMIT
 => true 
1.9.3p194 :003 > unit.inspect
 => "#<Unit id: nil, name: \"test\", created_at: \"2012-08-31 23:48:12\", updated_at: \"2012-08-31 23:48:12\">" 

# unit.rb
class Unit < ActiveRecord::Base
  attr_accessible :name
end

# migration
class CreateUnits < ActiveRecord::Migration
  def change
    create_table :units do |t|
      t.string  :name, :null => false

      t.timestamps
    end
  end
end

Tried this with other models and have the same result (no id). Data is definitily saved and i can get data with Unit.last

Another try with Foo.id = nil

# /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 :003 > foo.inspect
 => "#<Foo id: nil, name: \"bar\", created_at: \"2012-09-04 03:06:26\", updated_at: \"2012-09-04 03:06:26\">" 
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"> 

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord