0
我有一個問題,我希望有人能向我解釋這一點。例如關係:ActiveRecord,多對多的雙向關係
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:爲什麼只有創建方法添加記錄才能連接表?
在此先感謝。