Creating and Using Scopes In Active Admin

1.2k views Asked by At

How do i use scopes in Active Admin, In this example I am trying to display all members who's expiry date is equal to today's date.

class Member < ActiveRecord::Base
--- Code removed for brevity


#Scopes
  scope :expired_today, where(:expiry_date == Date.today)
end

Within the active admin dashboard i then want to display all those members who have expired

columns do

   column do
    panel "Expired Memberships", :class => 'expiredmemberships' do
     table_for Member.expired_today do 
       column("Name")  do |m| 
         "#{m.forename}  #{m.surname}" 
       end
     end
   end
  end 
 end

This is not working though, could someone give me a helping hand with an explanation on what needs to be done please

1

There are 1 answers

1
user229044 On BEST ANSWER

where works with scopes the same way it works everywhere else in Rails:

where(:expiry_date => Date.today)

You give it a hash with a key/value. Your way, using ==, basically invokes where(false), as the == isn't "passed in", it's resolved instantly (to false as a symbol and date will always be unequal) and the resulting value is passed to where.