Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ransack doesn't work with extended relations #578

Closed
bartoszkopinski opened this issue Aug 27, 2015 · 1 comment
Closed

Ransack doesn't work with extended relations #578

bartoszkopinski opened this issue Aug 27, 2015 · 1 comment

Comments

@bartoszkopinski
Copy link

Hi,
I'd like to extract some of my scopes out of a model and load them dynamically where needed using ActiveRecord::QueryMethods#extending method.

So I'm defining a sample scope module:

module AdminScopes
  def name_contains name
    where("full_name ILIKE ?", "%#{name}%")
  end

  def ransackable_scopes _
    super + [:name_contains]
  end
end

and this is how it works:

pry(main)> User.all.extending(AdminScopes).name_contains("Phill").count
   (55.4ms)  SELECT COUNT(*) FROM "users"  WHERE (full_name ILIKE '%Phill%')
=> 132

but when I try to use it with ransack I get:

pry(main)> User.all.extending(AdminScopes).ransack(name_contains: "Phill").result.count
NameError: undefined method `name_contains' for class `Class'
--
 0: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/context.rb:61:in `method'
 1: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/context.rb:61:in `scope_arity'
 2: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/search.rb:133:in `add_scope'
 3: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/search.rb:53:in `block in build'
 4: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/search.rb:47:in `each'
 5: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/search.rb:47:in `build'
 6: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/search.rb:39:in `initialize'
 7: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/adapters/active_record/base.rb:15:in `new'
 8: /.rvm/gems/ruby-2.2.2/gems/ransack-1.6.6/lib/ransack/adapters/active_record/base.rb:15:in `ransack'
 9: /.rvm/gems/ruby-2.2.2/gems/activerecord-4.1.12/lib/active_record/relation/delegation.rb:70:in `block in ransack'
10: /.rvm/gems/ruby-2.2.2/gems/activerecord-4.1.12/lib/active_record/relation.rb:292:in `scoping'
11: /.rvm/gems/ruby-2.2.2/gems/activerecord-4.1.12/lib/active_record/relation/delegation.rb:70:in `ransack'
12: (pry):61:in `<main>'

Is there a way to fix this or a workaround for keeping my models free of scoping responsibilities?

@scarroll32
Copy link
Member

This issue appears more to be a HOW-TO issue rather than a bug in Ransack. We are in the process of cleaning up the Ransack gem and repo. If this issue is still relevant could you please ask it on StackOverflow?

If you think it is an issue with Ransack, please provide a repo with the issue and / or a pull request with failing tests. If you don't know how to do this, indicate it in the issue and we will look into it eventually.

Thank you

https://github.com/activerecord-hackery/ransack/blob/master/CONTRIBUTING.md

#794

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants