2016-09-27 112 views
0

我設置了一個應用程序,並且遇到ActiveRecord關係問題。這裏是故障。Rails 4 ActiveRecord關係問題

1.因此客戶端的has_many站點一個站點belongs_to的客戶端

class Client 
has_many :sites 
end 

class Site 
belongs_to :client 
end 

2.一種網站的has_many報告因此belongs_to的網站一個報告

class Site 
has_many: reports 
end 

class Report 
belongs_to :site 
end 
  • 這是我陷入困境的地方。因此

    巡邏的has_many擊中命中belongs_to的巡邏

    belongs_to的網站一擊,因此站點的has_many點擊

    belongs_to的網站的一篇報道,因此站點的has_many報告

  • 問題設置這最後一部分不太確定如何佈置模型,或者如果通過關係保證?本質上,我希望在這裏實現的是,當管理員設置巡視併爲網站命中時,用戶可以通過熱門節目頁面查看網站並生成新的網站報告,然後在客戶端/管理員表中的網站顯示頁面。

    我在外太空嗎?

    嵌套客戶端和網站是一件輕而易舉的事情,我可以從網站顯示頁面生成一個新的報告,但爲了簡化最終用戶的報告創建,我希望去上面的路線..只是不知道如何繼續。

    如果您需要進一步的幫助,請讓我知道虐待給你我可以!

    感謝。

    編輯#1我的模型結構

    This group is nested as a site is built through the client show page 
    class Client 
    has_many :sites 
    end 
    
    class Site 
    belongs_to :client 
    end 
    

    這是我遇到的最troubel,因爲是在 在我心中,至少很多打算在這裏。我在配置 這個開放給任何建議..

    class PatrolRoute 
    has_many :patrol_hits 
    end 
    
    class PatrolHit 
    belongs_to :patrol_route 
    # A PatrolHit Should only have one Site and that site should not be created only selected from a dropdown box of pre-existing sites 
    end 
    
    class Site 
    belongs_to :patrol_hit 
    has_many: patrol_reports 
    end 
    
    Class PatrolReport 
    belongs_to :site 
    has_many :line_items 
    end 
    
    class LineItem 
    belongs_to :report 
    end 
    
    +0

    什麼是Patrol?另一種模式? – Aleksey

    +0

    虐待添加我的所有模型 –

    +0

    我只是問'巡邏'。這是這裏列出的一些模型的anotger模型或實例嗎? – Aleksey

    回答

    2

    這是很難爲你提供確切的答案,因爲整個事情,DB結構尚不清楚。

    有一兩件事可以幫助你處理所有這些關聯非常簡單:
    belongs_to :foo意味着模型的數據庫表應該有foo_id列。

    你的具體情況

    class PatrolHit 
        belongs_to :patrol_route 
    end 
    

    意味着patrol_hits表應該有像列

    id    
    patrol_route_id 
    ... 
    

    我認爲牢記規則會幫助你。