嗨gys我想嘗試鏈接兩個實體的一個實體是管理機構,遺產和repo_document,然後管理機構可以repo_document該遺產也可以有,所以我決定創建一個加入名爲document_owner表..但我不知道該怎麼在他們models..i已經寫在了我的document_owner模型這個代碼..需要幫助與多態聯接表在軌道
belongs_to :repo_document belongs_to :estate, :through => :repo_document, :foreign_key => :owner_id, :conditions => "owner_type = 'Estate'" belongs_to :governing_body, :through => :repo_document, :foreign_key => :owner_id, :conditions => "owner_type = 'GoverningBody'" belongs_to :owner, :polymorphic => true
而這個是我repo_document
has_and_belongs_to_many :owners, :join_table => :document_owners, :conditions => "owner_type = 'Estate' OR owner_type = 'GoverningBody'"
,這一次在我的莊園
has_many :repo_documents, :source => :document_owners, :foreign_key => :owner_id, :conditions => "owner_type = 'Estate' "
而這個是我governing_body
has_many :repo_documents, :source => :document_owners, :foreign_key => :owner_id, :conditions => "owner_type = 'GoverningBody' "
,但是當我試圖挽救它不保存連接表內的任何東西..
任何人可以幫助我,請
對於類似於圖像的東西,您可能會附加到多個對象,例如「事件」和「用戶?」,您有什麼建議? – 2013-02-04 19:46:34
如果這些是外圍葉型實體,那麼使用複合關鍵字和多態關聯很可能就像這裏例子中的Note一樣。當在JOIN中使用這些關係時,最好擁有像user_images或event_images這樣的特殊用途表,因爲這些索引在導航和更新上要便宜得多。這些擔憂實際上只與規模有關,比如超過10^6條記錄。 – tadman 2013-02-04 20:33:43