【发布时间】:2013-03-21 20:59:02
【问题描述】:
只是学习rails...
我有以下型号:
class TimeSlot < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::
has_and_belongs_to_many :time_slots
end
我也有模特加入两者:
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 may not be NULL: INSERT INTO "time_slots_users" ("time_slot_id", "user_id") VALUES (1, 1 )。
为什么 created_at 会为空?不是在创建 TimeSlotsUsers 中的记录时自动创建的吗?我是否需要改用 has-many through 关系?
【问题讨论】:
标签: ruby-on-rails-3.2 has-and-belongs-to-many