これは、Ruby on Rails と Active Record に関する初心者の質問です。Rails アプリ、sqlite テーブル、およびテーブルThing
にマップするために呼び出されるモデル クラスを作成しました。次に、以下のように Rails コンソールを実行しました。id
フィールドが正しく保存されないのはなぜですか?
irb(main):005:0> Thing.all
Thing Load (0.2ms) SELECT "things".* FROM "things"
=> []
irb(main):006:0> t = Thing.new :name => 'test'
=> #<Thing id: nil, name: "test", somedate: nil>
irb(main):007:0> t.id
=> nil
irb(main):009:0> t.save
(0.2ms) begin transaction
SQL (3.4ms) INSERT INTO "things" ("name", "somedate") VALUES (?, ?) [["name", "test"], ["somedate", nil]]
(0.8ms) commit transaction
=> true
irb(main):010:0> Thing.all
Thing Load (0.2ms) SELECT "things".* FROM "things"
=> [#<Thing id: nil, name: "test", somedate: nil>]
irb(main):011:0> t.id
=> 1
sqliteでは;
sqlite> .schema things
CREATE TABLE things (id int primary key, name text, somedate date);
モデル:
class Thing < ActiveRecord::Base
attr_accessible :name
end