谷歌搜索为此,我看到Rails核心团队正在为Rails 4开发一个解决方案,但这是一种解决方法。无法创建记录,因为连接表需要created_at(HABTM + Rails)
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也被称为“多对多”的关系。 – Kamil