レールを学ぶだけ...
私は次のモデルを持っています:
class TimeSlot < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::
has_and_belongs_to_many :time_slots
end
2つを結合するモデルもあります。
class TimeSlotsUsers < ActiveRecord::Base
attr_accessible :time_slot_id, :user_id
end
コンソールで、ユーザーオブジェクトを作成し、それをTimeSlotに関連付けたいと思います。ts
TimeSlotオブジェクトであり、ユーザーオブジェクトである変数がありu
ます。両方ともデータベースにすでに存在します。するとts.users << u
、「ActiveRecord :: StatementInvalid:SQLite3 :: ConstraintException:time_slots_users.created_atはNULLではない可能性があります:INSERT INTO "time_slots_users"( "time_slot_id"、 "user_id")VALUES(1、1)」というエラーが表示されます。
なぜcreated_atがnullになるのでしょうか。TimeSlotsUsersのレコードが作成されるときに、自動的に作成されませんか?代わりに、has-many through関係を使用する必要がありますか?