activerecord-hackery / squeel

Active Record, improved. Live again :)
http://erniemiller.org/2013/11/17/anyone-interested-in-activerecord-hackery/
MIT License
2.4k stars 214 forks source link

Squeel incompatible with Rails 4.2: Wrong number of arguments with `.find(id)` #352

Open doits opened 9 years ago

doits commented 9 years ago

Adding the squeel gem in Rails 4.2.0.rc2 and trying a basic find yields this:

[2] pry(main)> Task.find(1)
ArgumentError: wrong number of arguments (2 for 1)
from .../vendor/bundle/ruby/2.1.0/gems/activerecord-4.2.0.rc2/lib/active_record/relation/query_methods.rb:964:in `create_binds'

Looks like it must be updated for Rails 4.2.0?

bigxiang commented 9 years ago

Yes, I found the error too. It needs an update.

Envek commented 9 years ago

Such expressions are also broken: Category.where(name: 'some name here')

nulian commented 9 years ago

It is a pretty easy fix This below:

 def expand_attrs_from_hash(opts)
          opts = ::ActiveRecord::PredicateBuilder.resolve_column_aliases(klass, opts)

          bv_len = bind_values.length
          tmp_opts, bind_values = create_binds(opts, bv_len)
          self.bind_values += bind_values

          attributes = @klass.send(:expand_hash_conditions_for_aggregates, tmp_opts)
          attributes.values.grep(::ActiveRecord::Relation) do |rel|
            self.bind_values += rel.bind_values
          end

          attributes
        end

Has to become:

 def expand_attrs_from_hash(opts)
          opts = ::ActiveRecord::PredicateBuilder.resolve_column_aliases(klass, opts)

          tmp_opts, bind_values = create_binds(opts)
          self.bind_values += bind_values

          attributes = @klass.send(:expand_hash_conditions_for_aggregates, tmp_opts)
          attributes.values.grep(::ActiveRecord::Relation) do |rel|
            self.bind_values += rel.bind_values
          end

          attributes
        end
Envek commented 9 years ago

It seems to be already proposed in #354, so waiting until it gets merged and released.

Meanwhile it can be used from GitHub, change line in Gemfile to:

gem 'squeel', github: 'danielrhodes/squeel'

At least it works for me, thanks to @danielrhodes !

m-shirshendu commented 9 years ago

+1, getting this error on rails 4.2, and fixed by gem 'squeel', github: 'danielrhodes/squeel'

fake-or-dead commented 9 years ago

:+1: