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

Can't use ransack on a field called "item_type" #159

Closed
fernandomm opened this issue Nov 2, 2012 · 9 comments
Closed

Can't use ransack on a field called "item_type" #159

fernandomm opened this issue Nov 2, 2012 · 9 comments

Comments

@fernandomm
Copy link

Hi,

I have a field called item_type on a model called Version. When i try to use this:

<%= f.text_field :item_type_cont, :class => "input-medium search-query" %>

It throws an exception:

uninitialized constant Version::Item

It works fine for other fields. Any ideas of why this happens?

Thanks.

@radar
Copy link
Contributor

radar commented Nov 5, 2012

Which version of Ransack is this happening in?

Do you have a test Rails application that I can take a look at and
duplicate this problem?

On 3 November 2012 01:05, fernandomm notifications@github.com wrote:

Hi,

I have a field called item_type on a model called Version. When i try to
use this:

<%= f.text_field :item_type_cont, :class => "input-medium search-query" %>

It throws an exception:

uninitialized constant Version::Item

It works fine for other fields. Any ideas of why this happens?

Thanks.


Reply to this email directly or view it on GitHubhttps://github.com//issues/159.

@ScotterC
Copy link

I'm having the same issue with Rails 3.2.8, ruby 1.9.3 and Ransack 0.7.0. I'm trying to search id and type fields of a polymorphic model. For instance, the model is called DataError and has an errorable_id and errorable_type field. Attempting to search either of them gets a NameError: uninitialized constant DataError::Errorable

@radar
Copy link
Contributor

radar commented Nov 13, 2012

Do you have a dummy application that reproduces this issue? This would aid
us greatly in tracking down what is causing this issue.

On 14 November 2012 02:44, Scott Carleton notifications@github.com wrote:

I'm having the same issue with Rails 3.2.8, ruby 1.9.3 and Ransack 0.7.0.
I'm trying to search id and type fields of a polymorphic model. For
instance, the model is called DataError and has an errorable_id and
errorable_type field. Attempting to search either of them gets a NameError:
uninitialized constant DataError::Errorable


Reply to this email directly or view it on GitHubhttps://github.com//issues/159#issuecomment-10330682.

@ScotterC
Copy link

Sure thing.

https://github.com/ScotterC/ransack_polymorphic_bug

Instructions included in the readme

@Peleg
Copy link

Peleg commented May 24, 2013

I'm having the same issue... Any news on that?

@ascoppa
Copy link

ascoppa commented Jul 23, 2013

Same here 👎

Has anyone figured out a solution for this (or at least a workaround)?

Thanks

sineed pushed a commit to sineed/ransack that referenced this issue Nov 29, 2013
@sineed
Copy link

sineed commented Nov 29, 2013

I had the same issue and I found a WA: remove association from polymorphic model:

class Comment < ActiveRecord::Base
  # belongs_to :subject, polymorphic: true
end

It seems, that error occurs in view layer, I mean that

Comment.search(:subject_type_eq => "Article")

with uncommented belongs_to :subject works.

@radar, I wrote a test for this: #312

@sineed
Copy link

sineed commented Nov 30, 2013

I figured out this strange behavior. For example, I have in my form:

<%= f.text_field :notable_type_cont %>

This line calls respond_to? method in Ransack::Search and passes "notable_type_cont_before_type_cast" as first argument. Then it calls attribute_method? in Ransack::Nodes::Grouping. In this method before processing it strips unnecessary info from string. So in case when I do

Note.search(notable_type_cont: "Article")

string "notable_type_cont" became "notable_type". In first form case it doesn't happen because of "_before_type_cast".

So I think when it calls attribute_method? in Ransack::Context the string should look like notable_type, not notable_type_cont_before_type_cast.

@radar I wrote a fix for that, but I'm not sure if I'm doing right thing. Can you see it? #313

radar pushed a commit that referenced this issue Dec 4, 2013
@radar radar closed this as completed in f491cb2 Dec 4, 2013
sineed pushed a commit to sineed/ransack that referenced this issue Dec 9, 2013
@seanlinsley
Copy link
Contributor

Looks like this was the same error I was experiencing over here: #173 (comment)

jonatack added a commit that referenced this issue Dec 10, 2013
related with #159: remove `respond_to?` from Ransack::Search
jonatack added a commit that referenced this issue Dec 10, 2013
PR #316 related with #159: remove `respond_to?` from Ransack::Search
PR #317 Allow specifying default attribute and predicate via
options[:default]
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

7 participants