This project is archived and is in readonly mode.
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
-
Santiago Pastorino June 21st, 2010 @ 06:31 PM
- Milestone cleared.
- Assigned user set to Santiago Pastorino
-
Neeraj Singh June 21st, 2010 @ 09:43 PM
- Tag changed from rails3 arel activeresource bug to rails3 arel activeresource bug, patch
Attached is a test for the patch. Patch works fine.
-
Repository June 22nd, 2010 @ 05:09 PM
- 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 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/d0df7f1196ee633a0cea7b5b8cf8b6... -
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to Low
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>
People watching this ticket
Attachments
Referenced by
- 4917 Bug with passing an Arel::Relation into an ActiveResource predicate. (from [d0df7f1196ee633a0cea7b5b8cf8b65a8269df19]) Fix sma...