繁体   English   中英

如何使用作用域过滤active_admin中的多对多关联

[英]How can I use scope to filter many to many association in active_admin

我只想在索引页面中添加范围,以便我可以过滤legal_cases ,其中任何specific role都包含在它们的角色关联中,Role和LegalCase有我尝试过的多对多关联

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

但是我得到这个错误

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

任何帮助,请问我应该在这里使用什么而不是加入?

我这样用

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

向要过滤的模型添加范围,例如

class LegalCase < ActiveRecord::Base

  ...

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

我在我的应用程序中也有类似的情况

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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