0

我試圖爲CMS創建一個菜單,並且在它中,您有頂級部分(由不屬於另一部分確定的)具有列表更多部分或navigation_items。嘗試構建菜單的路徑,需要關於多態關聯的一些幫助

我無法全部連接。

我有以下型號:

section.rb:

class Section < ActiveRecord::Base 
    belongs_to :section, polymorphic: true 
    has_many :navigation_items 
end 

navigation_item.rb:

class NavigationItem < ActiveRecord::Base 
    belongs_to :section 
end 

我能請得到一些幫助,讓這一切連接,因爲我希望它上班?

+0

什麼是你的題? – DiegoSalazar

+0

'我無法全部連接'。 我認爲這是不言而喻的。 – Thermatix

回答

0

OK,所以我想通了,

我需要做的遷移,會做:

def change 
    change_table :sections do |t| 
     t.references :items, polymorphic: true, index: true, on_delete: :nullify, on_update: :cascade 
     t.references :section, index: true, on_delete: :nullify, on_update: :cascade 
    end 
end 

,然後類做:

class Section < ActiveRecord::Base 
    belongs_to :items, polymorphic: true 
    has_many :navigation_items, as: :items 
    has_many :sections, as: :items 
end 

class NavigationItem < ActiveRecord::Base 
    belongs_to :items, polymorphic: true 
end