在has_many通过关系上使用关联值来过滤活动管理中的表(Rails 4 / Active Admin)

时间:2014-04-20 18:51:43

标签: ruby-on-rails ruby ruby-on-rails-3 postgresql activeadmin

我有一个基本的has_many通过模型,客户可以有多个游戏:

客户模型属于合作伙伴,因此具有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'

在admin / 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

我的问题:我只想在上表中显示合作伙伴所属的合作伙伴IS EQUAL给客户的合作伙伴。

也就是说,如果你在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

2 个答案:

答案 0 :(得分:1)

考虑客户中的范围定义,定义游戏和客户之间的共享合作伙伴关系。您应该在合作伙伴模型中定义has_many :games关联(上面未提供)

例如customer.rb

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

然后是你的table_for

table_for customer.shared_partner_games

答案 1 :(得分:0)

尝试像这样更改table_for结构

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

我不确定。,让我知道它是否有效..