36
我有一個型號PointOfContact
其中has_many
Systems
。從Systems
方面我想確定PointOfContact
作爲technical_manager
或project_manager
(或兩者)。雖然仍然只在數據庫中保留PointOfContact
1次。相同型號的兩個belongs_to協會
我嘗試如下:
class System < ActiveRecord::Base
belongs_to :project_manager, :class_name => 'PointOfContact'
belongs_to :technical_manager, :class_name => 'PointOfContact'
end
class PointOfContact < ActiveRecord::Base
has_many :systems
end
當我跑我的規格(例如如下)我可以正確地創建System
點接觸關聯。但是,PointOfContact
不知道它與系統的關聯。這是爲什麼?
@sys = System.create
@tm = PointOfContact.create
@pm = PointOfContact.create
@sys.project_manager = @pm
@sys.technical_manager = @tm
@pm.systems.should have(1).items #> expected 1 items, got 0
更多詳情:通過關於[link](http://railscasts.com/episodes/163-self-referential-association?view=asciicast) – Zippie 2013-03-06 17:18:09