leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
536 stars 215 forks source link

2 associations to the same table (nested associations) #325

Open Obi-TOB opened 7 years ago

Obi-TOB commented 7 years ago

Hi,

I would really require some input :)

I have a model (a soccer match) with two associated teams (home_team, guest_team both of class Team). Both of them have translated names in an associated model Team::Translations (globalize3).

The soccer match has other associations as well (sport with its associated translations in Sport::Translations, competition with Competition::Translations, ...). These are working find with Wice_Grid. If I use only one of the associated teams elements are displayed as expected.

However if I have both home_team and guest_team assocations in Wice_Grid the entries of Team::Translations are only displayed in the column where they first occur.

So I think that AR gets confused as both nested associations (the translations) have the same model (and table_name) Team::Translation (team_translations).

So of course I tried to used the table_alias as described in the docs (which works fine as long as the attributes are directly in the Team association (home_team or guest_team of model Team). However I don't find a way to put an alias on the nested association (home_team.team_name / guest_team.team_name (both if model Team::Translation).

I have already passed quite a few hours stepping through the WiceGrid code, however, to be honest, the alias is my best guess, however not fully confirmed.

Any thoughts or ideas for workarounds would be highly appreciated.

I attached the definition of the grid. I'm using 3.6.0-pre4 (and tested as well on 3.6.0-pre5). Rails 4.2.5 and Ruby 2.2.5.

  g.column name: 'Sport',  attribute: 'sport_name', assoc: [:sport, :translations], detach_with_id: :id_sport, custom_filter: :auto do |match|
    match.sport.sport_name
  end

  g.column name: 'Competition', assoc: [:competition, :translations], attribute: 'competition_name', detach_with_id: :id_league, custom_filter: :auto do |match|
      match.competition.competition_name
  end

  g.column name: 'Season', assoc: [:season, :translations], attribute: 'season_name', detach_with_id: :id_season, custom_filter: :auto do |match|
    match.season.season_name
  end

  g.column name: 'Details', attribute: 'details', filter:false

  g.column name: 'Home Team', attribute: 'team_name', assoc:  [:home_team, :translations], filter:false do |match|
    match.home_team.team_name
  end

  g.column name: 'Guest Team', attribute: 'team_name', assoc: [:guest_team, :translations], table_alias: 'guest_team', filter:false do |match|
    match.guest_team.team_name
  end
Obi-TOB commented 7 years ago

I guess the easiest way to go would probably be to define a CustomViewColumn where I can "hardcode" the alias for the nested association. However I have difficulties to understand, where the value for the table field is actually selected (in the standard ViewColumns).

Could you point me in the right direction?

leikind commented 7 years ago

Sorry for this late reply.

WiceGrid does not support aliases for nested associations. An sql view is the only option here