【问题标题】:Use associated values on has_many through relations to filter a table in active admin (Rails 4/Active Admin)通过关系在 has_many 上使用关联值来过滤活动管理员中的表(Rails 4/Active Admin)
【发布时间】:2014-04-20 18:51:43
【问题描述】:

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

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

我的问题:我只想在上表中显示游戏,它所属的合作伙伴等于客户的合作伙伴。

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

【问题讨论】:

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


    【解决方案1】:

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

    例如customer.rb

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

    然后你的table_for

    table_for customer.shared_partner_games
    

    【讨论】:

    • 没用,我最后用了:table_for customer.customer_games.includes(:game, :customer).select{ |c| c.game.partner_id == c.customer.partner_id} 做|t|。成功了
    【解决方案2】:

    尝试像这样改变你的 table_for 结构

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

    不确定。如果有效,请告诉我。

    【讨论】:

    • 至少我现在没有收到错误,但我得到了我认为的所有游戏,而不仅仅是 customer_game.game.partner_id == customer_game.customer.partner_id 的游戏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 2015-11-16
    • 2012-02-27
    • 2013-05-06
    相关资源
    最近更新 更多