Skip to content

Creating dynamic scopes

amalagaura edited this page Jan 3, 2012 · 4 revisions

We can create dynamic scopes which are defined at initialization time (so not completely dynamic in production, the initializer will only be loaded at startup).

Here is an example where item_types are a category table and items belong to item_type through the item_type_id.

ActiveAdmin.register Item do
  scope :all, :default => true
  Category.all.each do |category|
    self.send(:scope, category.name) do |items|
      items.where(:category_id => category.id)
    end
  end
end
Clone this wiki locally