简体   繁体   中英

Ransack search through scope

I have a model Task with a scope named done . A Task belongs to a Project and to a User .

I need to filter users by projects in which they have a done task .

simple_form_for @q do |form|
  form.input :tasks_done_project_id, collection: Project.ordered

But using "_done" doesn't work.

I thought about a ransacker:

  ransacker :done do |parent|
    parent.done.table[:id]
  end

But doesn't work neither. Any Idea?

From the documentation :

By default, searching and sorting are authorized on any column of your model and no class methods/scopes are whitelisted.

...

 # `ransackable_scopes` by default returns an empty array # ie no class methods/scopes are authorized. # For overriding with a whitelist array of *symbols*. # def ransackable_scopes(auth_object = nil) [] end 

In your Task model, you must explicitly whitelist the done scope:

class Task < ActiveRecord::Base
  scope :done, -> { ... }

  def self.ransackable_scopes(auth_object = nil)
    [:done]
  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