infinitered / cdq

Core Data Query for RubyMotion
MIT License
172 stars 35 forks source link

sum(:a_field) does not work #103

Closed leadbaxter closed 9 years ago

leadbaxter commented 9 years ago

It could just be me (first time using CDQ) but I can't get sums to work.

I have a pretty simple Weather entity. No matter what I do, I get the same error.

schema '0002 added_extras' do
  entity 'Weather' do
    datetime :date,     optional: false
    string   :inches,   optional: false    # could be '1 1/2' or it could be '1.5'
    string   :month,    optional: true     # month number, like '06' or '12'
    float    :inches_f, optional: true      # is 1.5, 0.25, etc.
  end
end

# The field :inches_f is a float with inches of rainfall
# The field :month is a month, like '06' or '12'
Weather.where(:month).eq(month).sum(:inches_f)
Weather.sum(:inches).where(:month).eq(month)
Weather.sum(:inches_f)

After that, I would assume I could write:

total_for_month = Weather.where(:month).eq(month).sum(:inches_f).first.sum_of_inches_f

Here's the error resulting from any of the above queries:

targeted_query.rb:154:in `method_missing:': undefined method `sum' for <CDQ::CDQTargetedQuery:0x10adbe1e0 ...> (NoMethodError)
kemiller commented 9 years ago

I'm not sure why you're calling first on the result of sum. Sum should give you the totally all by itself. What precisely are you doing to get that error?

leadbaxter commented 9 years ago

The only reason I'd be calling first is because I don't know what I'm doing. I haven't been able to explore any further because the error stops me. If I could, then I would soon learn whether I needed to write .first or not!

I can reproduce the error as simply as:

Weather.sum(:inches_f)

Other than that, I'm doing all the CRUD operations, sorting, and grouping - the whole nine yards. But sum() does not work for me at all. Incidentally, I hit some other of the aggregate type functions that failed. But it was like 2am and I don't remember which ones.

kemiller commented 9 years ago

I guess I'm not sure what to say. You're right it should work, and that's an odd error. If you can put up a repo that will reliably produce that we can take a look.

leadbaxter commented 9 years ago

leadbaxter/cdq_test

I included a bunch of unit tests to show the problem, plus some other problems I was telling you I found, as well as a bunch of sanity tests (mostly because I wanted to see whether a lot of the Enumerable methods worked or not.)

A couple of things you may or may not care about when installing:

  1. I use RVM so there is a .ruby-gemset / .ruby-version pair of files (in case you care.)
  2. I added CDQ.cdq.reset! to the "before" section of tests to make sure the database starts off empty (if there was a crash, it didn't clear it out.)
  3. In the Rakefile, the id is com.leadbaxter.cdq_test (in case you care.)

If you need any more information just give me a holler!

Thanks for looking at it.

kemiller commented 9 years ago

Oh, OK. I see what's going on. You're using features that are in head, but not in the latest gem. I need to release v 1.0.3 apparently.

leadbaxter commented 9 years ago

That fixes all but the last two of my tests, the ones using the indexing. I modeled mine after what I saw in your tests, but it still didn't work.

Weather.sort_by(:readings)[0]

Any ideas?

leadbaxter commented 9 years ago

I figured out my problem... can't use negative indexes. Makes sense.

Weather[2]

instead of

Weather[-1]

works perfectly. I have 25 successful tests running.

Thanks for helping and for jumping right on it!