我有一个问题,我希望有人可以向我解释。示例关系:
class User < ActiveRecord::Base
has_many :issues
has_many :tasks, through: :issues
end
class Issue < ActiveRecord::Base
belongs_to :user
belongs_to :task
end
class Task < ActiveRecord::Base
has_many :issues
has_many :users, through: :issues
end
我将尝试通过关联创建一些模型:
user = User.create
task = user.tasks.new
user.tasks # [#<Task id: nil, created_at: nil, updated_at: nil>]
task.users # []
问题 1. 这是一种方式关联还是我做错了什么?
user.tasks.new.save
SQL (0.3ms) INSERT INTO "tasks" ("created_at", "updated_at") ...
user.tasks.create
SQL (0.4ms) INSERT INTO "tasks" ("created_at", "updated_at") ..
SQL (0.4ms) INSERT INTO "issues" ("created_at", "task_id", "updated_at", "user_id") ...
问题2:为什么只有create方法添加记录到连接表?
提前致谢。