This project is archived and is in readonly mode.
ActiveRecord::Relation#empty? fails for grouped relations, as OrderedHash is missing #zero?
-
Dalibor Filus
Today i came over this error too...
Using Rails 3.0.3, Ruby 1.8.7 Enterprise Edition# Showing /var/www/truhlari/app/views/articles/_detail.html.haml where line #103 raised: # undefined method `zero?' for #<OrderedHash {"570"=>1, "683"=>1, "551"=>1, "553"=>1}> # causing line in view: if article.more_articles.active.published.any? # model: def more_articles ids = self.categories.collect(&:id).compact return self.class.scoped.joins("join e3_assocs on `item2_id` = `e3_models`.`id`").where("`item1_id` IN (#{ids.compact.join(',')})").group('`e3_models`.`id`') endCalling .empty? instead of .any? does the same...
It seems to be solved for my case by using .select("DISTINCT()...")
But it would not be so easy-solvable for different code cases .. :/
-
mixr
This is quite annoying. But since most results are iterated over anyway, you could just use .to_a.empty? or .all.empty?:
(of course this is only sensible when limit() is involved)@articles = Article.published.group("publisher_id").limit(10).to_a.empty?Not to speak of other problems with using count() on your ActiveRecord::Relation when limit() or offset() is involved.
You just cannot use a count/size on a scope and rely on the result.
for example see: #4882 ActiveRelation bug; size() returning incorrect value with limit(). -
gamov
Same issue for me with the following scope:
scope :quantity_for_bs, lambda {|bs_id|{ :joins => :shipment, :select => '*, SUM (quantity) as total_quantity', :group => :item_variant_id, :conditions => {:shipments => {:dest_site_id => bs_id}} }}Then if I do
shi = ShippingItem.transiting.quantity_for_bs(@business_site.id) shi.empty? # <- raises undefined method `zero?' for {}:ActiveSupport::OrderedHashInterestingly, if I do shi.inspect in between the statements, then .empty? resolves correctly...
-
Dan Pickett
- State changed from new to open
- Milestone set to 3.0.6
- Importance changed from to Low
Chris Oliver has created a pull request that can help to resolve this issue: https://github.com/rails/rails/pull/218
-
Repository
- State changed from open to resolved
(from [3a22e744162ce4d29f228e76d3e3ff355c93b88f]) Fixed a bug when empty? was called on a grouped Relation that wasn't loaded
[#5829 ActiveRecord::Relation#empty? fails for grouped relations, as OrderedHash is ... state:resolved] https://github.com/rails/rails/commit/3a22e744162ce4d29f228e76d3e3f...
-
Chris Oliver
(from [015192560b7e81639430d7e46c410bf6a3cd9223]) Fixed a bug when empty? was called on a grouped Relation that wasn't loaded
[#5829 ActiveRecord::Relation#empty? fails for grouped relations, as OrderedHash is ... state:resolved]
Signed-off-by: Andrew White andyw@pixeltrix.co.uk
https://github.com/rails/rails/commit/015192560b7e81639430d7e46c410...