On Feb 14, 5:47 pm, cult hero <[email protected]> wrote:
> Perhaps I'm miss understanding something but, it seems that if I
> combine a filter and associations, I run into a world problems.
>
> I have what I think is a pretty simple association in a model:
>
> -----
> class AssetEvent < Sequel::Model
>
>   set_dataset dataset.order(:date_start, :date_circa.desc)
>   many_to_one :base, :class => Asset, :key
> => :asset_id, :graph_join_type => :inner
>
>   def year()
>     self.date_start.year
>   end
>
>   ... (omitted)
> end
> -----
>
> (date_start is just a Date field in MySQL)
>
> Example 1: Eager loading the association.
>
> @events = AssetEvent.filter(:date_start >= DateTime.new
> (start_year)).filter(:date_start < DateTime.new(end_year)).eager_graph
> (:base)
>
> This loads everything all right, but if I iterate though events
> using .each I get this error when I try to call my year method:
>
> undefined method `year' for #<Hash:0x19f602c>

You have to call #all when you use eager graph, you can't iterate
through the records with #each.  In order to eagerly load records, you
have to load all the main reecords at once

> So... it's returning a hash instead of a normal dataset?

Yes.  If you call #each on an eager_graphed (or any graphed dataset),
you get a hash of subhashes.  You need to call #all to get it to
process the array of hashes with subhashes and return an array of
objects with the associations you are eagerly loading already cached.

> Example 2: Fine, I won't do an eager load and see if that solves it.
>
> @events = AssetEvent.filter(:date_start >= DateTime.new
> (start_year)).filter(:date_start < DateTime.new(end_year))
>
> So I iterate using .each and...
>
> Mysql::Error Commands out of sync; you can't run this command now
>
> What really confuses me is that both of these work just fine if I
> don't use .filter and just grab the whole table. I've checked to make
> sure the SQL produced by the filter statements is good, and it is so
> I'm totally confused. Does filtering a model kill all its methods or
> what?

This is due to the MySQL adapter not handling nested queries.  Again,
using #all instead of #each will fix it (which I think you've found
out).

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to