Ransack: Searching by association_attribute_not_eq does not work.

Created on 6 Feb 2018  路  2Comments  路  Source: activerecord-hackery/ransack

Hello, I tried this example by calling binding.pry from the rspec after encountering the issue from my repository.

Person.search(notes_status_eq: 'temp').result.to_sql
=> "SELECT \"people\".* FROM \"people\" LEFT OUTER JOIN \"notes\" ON \"notes\".\"notable_id\" = \"people\".\"id\" AND \"notes\".\"notable_type\" = 'Person' WHERE \"notes\".\"status\" = 'temp' ORDER BY \"people\".\"id\" DESC"
Person.search(notes_status_not_eq: 'temp').result.to_sql
NoMethodError: undefined method `left' for #<ActiveRecord::Relation::QueryAttribute:0x007fc4d02a67b8>
from /path/to/activerecord-hackery/ransack/lib/ransack/adapters/active_record/context.rb:190:in `block in build_correlated_subquery'

In case of my repository, the search returns another error :

Offer.search(reservations_status_not_eq: 'temp').result.to_sql
=> NoMethodError - undefined method `eq' for #<Arel::Nodes::Equality:0x007fa7c0fb5eb8>:
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/ransack-1.8.4/lib/ransack/adapters/active_record/context.rb:192:in `build_correlated_subquery'

with version

ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
Rails 5.1.4
ransack 1.8.4

I know that this may not contain enough information, but I'll leave it as it is for the time being.
I'll dig deeper by this week unless any of you can correct my problem.

Thanks in advance.

tests wanted

Most helpful comment

Hi @seanfcarroll, it's been a while.

Here is my model :

class User < ApplicationRecord

end

# == Schema Information
#
# Table name: users
#
#  id                               :integer          not null, primary key
#  email                            :string(255)      default(""), not null


class Reservation < ApplicationRecord
  belongs_to :user
end

# == Schema Information
#
# Table name: reservations
#
#  id                               :integer          not null, primary key
#  begin_at                         :date
#  user_id                          :integer
#  status                           :string(255)

My Controller :

module Manager
  module CRM
    class UsersController < Manager::ManagerController
      before_action :authenticate_operator!
      read_only only: [:index, :show]

      def index
        if params[:q]
          params[:q][:s] = 'id desc' if params[:q] && params[:q][:s].blank?
          @search = User.search(params[:q])
          ...
          ...
          ...
        end
      end
    end
  end
end
[9] pry(main)> params = {"q":{"reservations_status_eq":"temp"}}
=> {:q=>{:reservations_status_eq=>"temp"}}
[10] pry(main)> User.search(params[:q]).result.count
   (7608.8ms)  SELECT COUNT(*) FROM "users" LEFT OUTER JOIN "reservations" ON "reservations"."user_id" = "users"."id" AND "reservations"."deleted_at" IS NULL AND (reservations.user_id IS NOT NULL) WHERE "users"."deleted_at" IS NULL AND "reservations"."status" = 'temp'
=> 659


[11] pry(main)> params = {"q":{"reservations_status_not_eq":"temp"}}
=> {:q=>{:reservations_status_not_eq=>"temp"}}
[12] pry(main)> User.search(params[:q]).result.count
NoMethodError: undefined method `eq' for #<Arel::Nodes::Equality:0x000000105f1120>
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/ransack-1.8.4/lib/ransack/adapters/active_record/context.rb:192:in `build_correlated_subquery'

All 2 comments

Hi @zoonoo could you post your model and controller code?

Hi @seanfcarroll, it's been a while.

Here is my model :

class User < ApplicationRecord

end

# == Schema Information
#
# Table name: users
#
#  id                               :integer          not null, primary key
#  email                            :string(255)      default(""), not null


class Reservation < ApplicationRecord
  belongs_to :user
end

# == Schema Information
#
# Table name: reservations
#
#  id                               :integer          not null, primary key
#  begin_at                         :date
#  user_id                          :integer
#  status                           :string(255)

My Controller :

module Manager
  module CRM
    class UsersController < Manager::ManagerController
      before_action :authenticate_operator!
      read_only only: [:index, :show]

      def index
        if params[:q]
          params[:q][:s] = 'id desc' if params[:q] && params[:q][:s].blank?
          @search = User.search(params[:q])
          ...
          ...
          ...
        end
      end
    end
  end
end
[9] pry(main)> params = {"q":{"reservations_status_eq":"temp"}}
=> {:q=>{:reservations_status_eq=>"temp"}}
[10] pry(main)> User.search(params[:q]).result.count
   (7608.8ms)  SELECT COUNT(*) FROM "users" LEFT OUTER JOIN "reservations" ON "reservations"."user_id" = "users"."id" AND "reservations"."deleted_at" IS NULL AND (reservations.user_id IS NOT NULL) WHERE "users"."deleted_at" IS NULL AND "reservations"."status" = 'temp'
=> 659


[11] pry(main)> params = {"q":{"reservations_status_not_eq":"temp"}}
=> {:q=>{:reservations_status_not_eq=>"temp"}}
[12] pry(main)> User.search(params[:q]).result.count
NoMethodError: undefined method `eq' for #<Arel::Nodes::Equality:0x000000105f1120>
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/ransack-1.8.4/lib/ransack/adapters/active_record/context.rb:192:in `build_correlated_subquery'
Was this page helpful?
0 / 5 - 0 ratings