I would whack a "debugger" or "binding.pry" in that code and follow it line 
by line and inspedct the variables and code-flow to see if its even going 
in the find each loop, then what is locatable_type's value, Take.name, etc.

Installing pry or debugger is real easy.

PS. If you want != and other fancy SQL syntactic sugar I use squeel

On Friday, October 12, 2012 11:01:00 AM UTC+10:30, 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).count}"
>
>     locations.find_each do |loc|
>       puts "Found a take!" if loc.locatable_type == 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 view this discussion on the web visit 
https://groups.google.com/d/msg/rails-oceania/-/bm7sb_dLrW8J.
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