Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#4917

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.

Reported by James Harton · June 21st, 2010 @ 03:19 AM

State: resolved
Milestone: 3.0.2
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. James Harton
  2. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • Assigned user set to Santiago Pastorino

    June 21st, 2010 @ 06:31 PM

  3. Neeraj Singh
    Neeraj Singh
    • Tag changed from rails3 arel activeresource bug to rails3 arel activeresource bug, patch

    Attached is a test for the patch. Patch works fine.

    June 21st, 2010 @ 09:43 PM

  4. Repository
    Repository
    • State changed from new to resolved

    (from [d0df7f1196ee633a0cea7b5b8cf8b65a8269df19]) Fix small bug where ActiveRecord::PredicateBuilder#build_from_hash didn't test for Arel::Relation as right hand value. [#4917 Bug with passing an Arel::Relation into an ActiveResource predicate. state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/d0df7f1196ee633a0cea7b5b8cf8b6...

    June 22nd, 2010 @ 05:09 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM