Just a hunch based off what you've provided, but have you got a default scope 
for a different value of locatable_type?
Or could locatable_type and Take.name be different but not comparable by ruby 
types ? 


Jon Rowe
-----------------------------
[email protected]
http://jonrowe.co.uk



On Friday, 12 October 2012 at 01:31, Ben Taylor wrote:

> Hey there roro-ers,
> 
> I'm getting some weird querying inconsistencies and I'm wondering if anyone 
> can help me out. I have a model called "Location" which has a polymorphic 
> association with a "Locatable". 
> 
> One of our models didn't correctly create it's associated Location objects, 
> so I retro-actively created them like so:
> 
> Take.find_each do |t| 
>   Location.create(
>     locatable: t, 
>     latitude: t.latitude, 
>     longitude: t.longitude, 
>     created_at: t.created_at, 
>     updated_at: t.updated_at
>   ) 
> end
> 
> In this case I was lucky because the Take object is only ever located once 
> (unlike our Player objects which are located multiple times).
> 
> Now I'm currently running a script which visualises activity in our game. It 
> goes like this:
> 
>          frame = []
>     locations = game.locations.where("created_at > ? AND created_at < ?", 
> start, finish)
>     locations = locations.order("created_at ASC")
>     puts "Count of takes: #{locations.where(locatable_type: Take.name 
> (http://Take.name)).count}"
> 
>     locations.find_each do |loc|
>       puts "Found a take!" if loc.locatable_type == Take.name 
> (http://Take.name)
>       if loc.created_at < current
>         frame << loc
>       else
>         while loc.created_at >= current
>           write(game, frame, current, frame_length)
>           current += frame_length 
>           frame = []
>         end
>         frame << loc
>       end
>     end
> 
> 
> So it is querying the DB for all locations between the start and end of the 
> game, then putting them into frame_length chunks. The strange behaviour I'm 
> seeing is that in our output there are no Locations with locatable_type 
> "Take". But when I query the database, they are definitely there. The output 
> from running this is:
> 
> Doing
> Count of takes: 693
> Done
> 
> Which indicates that locatable_type is never Take, but as per the query 
> before that they definitely are there.
> 
> Any ideas about what I'm doing wrong?
> 
>  - Ben
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected] 
> (mailto:[email protected]).
> To unsubscribe from this group, send email to 
> [email protected] 
> (mailto:[email protected]).
> For more options, visit this group at 
> http://groups.google.com/group/rails-oceania?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" 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/rails-oceania?hl=en.

Reply via email to