在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
然而,他们也提到:
另请注意,范围考虑了保存的版本 记录,不考虑你可能在中做出的任何未保存的更改 编辑表格。如果你改变球队的联赛,你仍然会看到 来自旧联盟的球员,直到你拯救。
使新的未保存记录的最佳方法是什么?因此,当我在表单中更改联赛时,玩家将相应地更新,而无需保存记录。