[英]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 我只想在索引页面中添加范围,以便我可以过滤legal_cases
,其中任何specific role
都包含在它们的角色关联中,Role和LegalCase有我尝试过的多对多关联
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.