> How is Game model defined? My guess is that "game.locations" adds WHERE
> locatable_type='Game' to query clause.
>
>
Sorry, to clarify a Game has_many :locations. A game itself isn't locatable.
Other locatable types are coming through, just not Take.
> Of course all these are just guesses cause I dont now how are your models
> defined
Yeah, it's kind of hard to summarise the whole thing really quickly. I figured
it was better to try and highlight the important bits, than try to show you my
entire structure.
> One trick - instead of
>
> where("created_at > ? AND created_at < ?", start, finish)
>
> you can do
>
> where(created_at: start..finish)
Neat trick. I'll remember that. Is there a similar way to get a != version of a
query? (risking going off topic)
- Ben
On Friday, 12 October 2012 at 4:47 PM, Krasimir Angelov wrote:
> Have you checked actual SQL generated in the logs?
>
> How is Game model defined? My guess is that "game.locations" adds WHERE
> locatable_type='Game' to query clause. The second query finds them cause
> locatable_type is set explicitly and overrides the former. Of course all
> these are just guesses cause I dont now how are your models defined.
>
> One trick - instead of
>
> where("created_at > ? AND created_at < ?", start, finish)
>
> you can do
>
> where(created_at: start..finish)
>
> Regards,
> K.
>
>
> On Friday, October 12, 2012 3:31:00 AM UTC+3, 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 view this discussion on the web visit
> https://groups.google.com/d/msg/rails-oceania/-/UGKnlmGKucsJ.
> 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.