5

在 Rails_admin wiki中,他们描述了关联范围如何工作:

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's block!
      team = bindings[:object]
      Proc.new { |scope|
        # scoping all Players currently, let's limit them to the team's 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

但是,他们也提到:

另请注意,范围考虑了记录的已保存版本,而不考虑您可能在编辑表单中所做的任何未保存的更改。如果您更改球队的联赛,您仍然会看到旧联赛的球员,直到您保存为止。

使这项工作也适用于未保存的新记录的最佳方法是什么?所以当我在表格中更改联赛时,球员会相应地更新,而不必保存记录。

4

0 回答 0