简体   繁体   中英

How can I use scope to filter many to many association in active_admin

I just want to add scope in my index page so I can filter legal_cases which any specific role is included in their roles association, Role and LegalCase have many to many association I tried

ActiveAdmin.register LegalCase do
  scope :film_maker, joins(:roles).where('roles.name = ?', "Film Maker")
end

but I got this error

undefined method `joins' for #<ActiveAdmin::ResourceDSL:0x9ca28f4>

any help please what should I use here instead of join?

I used one like this

ActiveAdmin.register LegalCase do

  controller do
    def scoped_collection
      end_of_association_chain.joins("left join users on users.id = bookings.user_id").select('bookings.*, users.*').order('users.name asc')
    end
  end
end

add a scope to the model you're filtering, eg

class LegalCase < ActiveRecord::Base

  ...

  scope :film_makers, -> { joins(:roles).where(roles: { name: 'Film Maker' }) }
end

I had a similar case I am doing this in my application

ActiveAdmin.register Restaurant do
  scope("All"){|scope| scope.order("created_at desc")}

    Cuisine.all.each do |c|
      scope(c.name) { |scope| scope.joins(:cuisines).where("cuisines.id=?",c.id)}
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM