Alex. b
Alex. b

Reputation: 689

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?

Upvotes: 1

Views: 4860

Answers (1)

Tom Lord
Tom Lord

Reputation: 28305

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
# i.e. 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

Upvotes: 7

Related Questions