Skip to content

Creating dynamic scopes

amalagaura edited this page Dec 5, 2011 · 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 and items belong to item_type through the item_type_id.

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