【发布时间】:2012-10-26 06:32:39
【问题描述】:
在谷歌上搜索,我看到 Rails 核心团队正在为 Rails 4 开发解决方案,但那还有很长的路要走。
Users 和 Circles 都具有has_and_belongs_to_many 关系。
我的架构如下所示:
create_table "circles", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "circles_users", :force => true do |t|
t.integer "circle_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password"
end
但在我的代码中,在我的circles_controller 中,当我尝试这样做时:
def create
@circle = Circle.new(params[:circle])
@circle.users << User.find(session[:user].id)
...
我的浏览器出现以下错误:
SQLite3::ConstraintException: circles_users.created_at may not be NULL: INSERT INTO "circles_users" ("circle_id", "user_id") VALUES (11, 5)
created_at 和 updated_at 在这里是假的,我该怎么办?
【问题讨论】:
-
供您参考:has_and_belongs_to_many 也称为“多对多”关系。
标签: ruby-on-rails sqlite migration schema has-and-belongs-to-many