Association detection for new / unsaved entries in Rails_admin

In the Rails_admin wiki, they describe how the binding area can work:

config.model Team do
  field :players do
    associated_collection_cache_all false  # REQUIRED if you want to SORT the list as below
    associated_collection_scope do
      # bindings[:object] & bindings[:controller] are available, but not in scope block!
      team = bindings[:object]
      Proc.new { |scope|
        # scoping all Players currently, let limit them to the team league
        # Be sure to limit if there are a lot of Players and order them by position
        scope = scope.where(league_id: team.league_id) if team.present?
        scope = scope.limit(30).reorder('players.position DESC') # REorder, not ORDER
      }
    end
  end
end

However, they also mention:

Also note that the scope takes into account the saved version of the record, not counting any unsaved changes that you might have made in the edit form. If you change the team championship, you will still see players from the old league until you save.

What is the best way to make this work for new, unsaved records too? Therefore, when I change the league in the form, the players will be updated accordingly, without saving the record.

+5
source share

All Articles