简体   繁体   中英

Use associated values on has_many through relations to filter a table in active admin (Rails 4/Active Admin)

I have a basic has_many through models where a customer can have multiple games:

The Customer model belongs to a Partner and consequently has a partner_id column, same for the game model that belongs to a Partner.

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'

In admin/customer.rb, I have a (working) table giving me details for all the games of a specific customer . I get it this way:

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

My question: I'd like to only display on the above table the Games WHERE the Partner it belongs to IS EQUAL to the partner of the CUSTOMER.

That is to say if you take a line in the customer_games table with games_id and customer_id, and if I go to check the partner_id linked with game_id and then the partner_id associated with the customer_id, if they are equal, then this game can appear in my table.

Quite hard to explain in words, I'm sorry.

For reference, my last attempt what i tried but it does not work:

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

consider a scope definition in customer that defines this shared partner relationship between games and customers. You should have a definition has_many :games association in the Partner model ( which is not provided above)

for example customer.rb

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

then your table_for :

table_for customer.shared_partner_games

Try to change your table_for structure like this

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

Am not sure., let me know if it works..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM