ransack
ransack copied to clipboard
ransackable_attributes bug when searching association
Hi,
Here's a sample project:
# has :name attribute
class Property < ActiveRecord::Base
has_many :product_properties
def self.ransackable_attributes(auth_object = nil)
['name']
end
end
# has :property_id attribute
class ProductProperty < ActiveRecord::Base
belongs_to :property
def self.ransackable_attributes(auth_object = nil)
['property_name']
end
end
Running:
Property.first.product_properties.ransack(property_name_cont: 'asd').result
Results in:
NoMethodError:
undefined method `type' for nil:NilClass
# /Users/viktorfonic/.rvm/gems/ruby-2.3.3/bundler/gems/ransack-4adfd103983c/lib/ransack/adapters/active_record/context.rb:32:in `type_for'
When I remove ransackable_attributes
, the search works as expected.
Line 32 of context.rb
: schema_cache.columns_hash(table)[name].type
I've attached debugger and evaluated the variables:
> table
=> "product_properties"
> name
=> "property_name"
> schema_cache.columns_hash(table)[name]
=> nil
> schema_cache.columns_hash(table)
=> {"id"=>
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f9c21198628
@array=false,
@cast_type=#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer:0x007f9c21123f80 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,
@default=nil,
@default_function="nextval('product_properties_id_seq'::regclass)",
@name="id",
@null=false,
@sql_type="integer">,
"property_id"=>
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f9c211984c0
@array=false,
@cast_type=#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer:0x007f9c21123f80 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,
@default=nil,
@default_function=nil,
@name="property_id",
@null=true,
@sql_type="integer">,
"created_at"=>
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f9c21198358
@array=false,
@cast_type=#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime:0x007f9c21123760 @limit=nil, @precision=nil, @scale=nil>,
@default=nil,
@default_function=nil,
@name="created_at",
@null=false,
@sql_type="timestamp without time zone">,
"updated_at"=>
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f9c211981f0
@array=false,
@cast_type=#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime:0x007f9c21123760 @limit=nil, @precision=nil, @scale=nil>,
@default=nil,
@default_function=nil,
@name="updated_at",
@null=false,
@sql_type="timestamp without time zone">}
Hope it helps.
I also created a sample project that shows this error: https://github.com/vfonic/ransacked
When you clone the project, run:
bundle install
rake db:create
rake db:migrate
rspec
@vfonic I'm seeing this error myself today, did you ever find a workaround for it?
No, ended up not needing it.
On May 9, 2017 19:50, "Robert Rawlins" [email protected] wrote:
@vfonic https://github.com/vfonic I'm seeing this error myself today, did you ever find a workaround for it?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/activerecord-hackery/ransack/issues/756#issuecomment-300152728, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEHbQ8u1I4W2dDgLX0o0Lg8yebUH6V7ks5r4GEugaJpZM4L06Hv .
@seanfcarroll I'm trying to find a PR that fixes this. Was this fixed?
Or is there any reason for closing unresolved issues, with full repro steps and an example project?
@vfonic sorry I thought it was not needed. I'm trying to clean up the many issues in Ransack. If it's still there can you submit a repo or a pull request with failing tests? It will make my job a little easier ...
Cheers
@seanfcarroll this is the repo: https://github.com/vfonic/ransacked
It has a spec written in the repo. I don't have any time atm to make it into a spec directly in the ransack repo. Sorry!
Thanks a lot!
I caught this bug a while ago. However, I used ransack_alias instead of ransackable_attributes. Likewise, it seems to happen when traversing relationships.
I'm also facing the same issue.
added a method in a model like this
def self.ransackable_attributes(auth_object = nil)
%w[orderId]
end
Output
#<ActionController::Parameters {"orderId_eq"=>"3"} permitted: false>
(byebug) @orders.ransack(params[:query])
*** NoMethodError Exception: undefined method `type' for nil:NilClass
nil
any solution for this.
Hi!
I think ransackable_attributes
is expected to only include column names, not association names, ransackable_associations
should be the one to override for associations. Changing the ransackable_attributes
in the original repro to return an empty array fixes the issue.
I guess we should add some validation here?