SFEley/candy

Items returned by Candy::Collection shouldn't cache all values as nil

Opened this issue · 4 comments

class Email
  include Candy::Piece
end

class Emails
  include Candy::Collection
  collects Email
end

Emails.each do |email|
  email.provider # => nil
  email.refresh
  email.provider # => "some value" 
end

Imho, it shouldn't be required to manually call email.refresh

Good catch. I'll investigate what's going on there, thanks.

Same here. I'm using MongoDB 1.4.4 on Ubuntu 10.04, with Candy 0.2.10.

I found the issue here. Its in the initializer for the Collection::ClassMethods

Line 62 of collection.rb

if conditions.is_a?(Hash)
    @_candy_options = {:fields => '_id'}.merge(extract_options(conditions))
    @_candy_query.merge!(conditions)
  else
    @_candy_options = {:fields => '_id'}
  end

The problem is that by default conditions is set to {} so Candy always pulls just the _id field, hence all other attributes are nil.

I'm not really sure how to fix it without brute forcing @_candy_options back to {}.

I forked Ben Hosking's updated repo and made a fix that corrects this issue.