2014-04-20 25 views
2

我不得不通過模式有一個基本的has_many其中一個客戶可以有多種遊戲:使用相關值,以主動管理篩選表(軌道4 /主動聯繫)

客戶模型屬於合作伙伴並且因此具有partner_id列,對於屬於合作伙伴的遊戲模型也是如此。

models/customer.rb 
has_many :customer_games 
has_many :games,    through: :customer_games 
belongs_to :partner,   :foreign_key => 'partner_id' 

models/game.rb 
has_many :customer_games 
has_many :customers,  through: :customer_games 
belongs_to :partner,  :foreign_key => 'partner_id' 

models/customer_games.rb 
belongs_to :customer,  :foreign_key => 'customer_id' 
belongs_to :game,   :foreign_key => 'game_id' 

在管理/ customer.rb,我有(工作)表給我細節全部特定客戶的遊戲。我得到這樣說:

panel "Games and infos of these games for this customer:" do 
     table_for customer.customer_games do |t| 
     t.column("Name")  { |customer_game| link_to customer_game.game.title, admin_game_path(customer_game.game), target: :blank } 
     t.column("Partner")  { |customer_game| if customer_game.game.partner.name.present? 
                link_to customer_game.game.partner.name, admin_partner_path(customer_game.game.partner), target: :blank 
               else 
                status_tag('Empty') 
               end 
           } 
     t.column("Country") { |customer_game| customer_game.game.country }  
     end 
    end 

我的問題:我想只在上表中顯示遊戲中的合作伙伴屬於等於給客戶的合作伙伴。

也就是說,如果您在customer_games表中使用了games_id和customer_id,並且如果我檢查與game_id鏈接的partner_id以及與customer_id相關聯的partner_id,如果它們相等,則此遊戲可以出現在我的表格中。

很難用言語解釋,對不起。

僅供參考,我的最後一次嘗試我試過,但它不工作:

panel "Games and infos of these games for this customer:" do 

     table_for customer.customer_games.where(game_id.game.partner_id == customer_id.customer.partner_id) do |t| 
     t.column("Game Name") { |customer_game| link_to customer_game.game.title, admin_game_path(customer_game.game), target: :blank } 
     t.column("Partner")  { |customer_game| if customer_game.game.partner.name.present? 
                link_to customer_game.game.partner.name, admin_partner_path(customer_game.game.partner), target: :blank 
               else 
                status_tag('Empty') 
               end 
           } 
     t.column("Country") { |customer_game| customer_game.game.country } 

     end 
    end 

回答

0

試圖改變自己的table_for結構這樣

table_for customer.customer_games.where(customer.partner。 id == Game.find_by_partner_id(customer.partner.id).partner.id)do | t |

我不知道,讓我知道,如果它的工作原理..

+0

至少我沒有得到一個錯誤現在,但我得到所有我認爲的遊戲,而不僅僅是那些customer_game.game.partner_id == customer_game.customer.partner_id – Mathieu

1

考慮客戶的一個範圍定義,定義了遊戲與客戶之間的這種共享的合作伙伴關係。你應該在合作伙伴模型的定義has_many :games協會(未上面提供)

例如customer.rb

has_many :shared_partner_games, :through => :partner, :source => games 

那麼你table_for

table_for customer.shared_partner_games 
+0

沒有工作,我在最後使用:table_for customer.customer_game s.includes(:game,:customer).select {| c | c.game.partner_id == c.customer.partner_id} do | t |。它工作 – Mathieu

+0

最接近的解決方案! – Mathieu