This project is archived and is in readonly mode.

#4917 ✓resolved
James Harton

Bug with passing an Arel::Relation into an ActiveResource predicate.

Reported by James Harton | June 21st, 2010 @ 03:19 AM | in 3.0.2

Suppose you have a fairly complex query that you have built using Arel, and now you want to pass it into a where predicate to get your AR models back (here is a simple example, but could be any number of outer joins, etc):

pt = Post.arel_table
current = pt.where(pt[:created_at].gt(1.day.ago)).project(pt[:id])
current.to_a.size
 => 6
Post.where(:id => current).size
 => 1

This is because ActiveRecord::PredicateBuilder#build_from_hash doesn't test for Arel::Relation and thus defaults to eq() instead of in():

      case value
      when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Relation
        values = value.to_a
        attribute.in(values)
      when Range
        attribute.in(value)
      else
        attribute.eq(value)
      end

Resolution is to modify the line when Range to when Range, Arel::Relation. Patch forthcoming.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages