在Rails_admin中关联新范围/未保存记录的范围

use*_*924 5 ruby-on-rails ruby-on-rails-3 rails-admin

在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
Run Code Online (Sandbox Code Playgroud)

但是,他们还提到:

另请注意,范围考虑了记录的保存版本,而不考虑您在编辑表单中可能进行的任何未保存的更改.如果你改变球队的联赛,你仍然可以看到老联盟的球员,直到你保存.

对于新的未保存的记录,这也是最好的方法吗?因此,当我在表单中更改联盟时,玩家将相应地更新,而无需保存记录.