
STI association children query returns nil (WHERE type is wrong)
Reported by Peter | August 11th, 2008 @ 05:25 PM
Models:
class Gallery include DataMapper::Resource property :id, Integer, :serial => true property :title, String, :length => 1..255 property :description, Text property :published, Boolean, :default => false
has n, :photos end
class Asset include DataMapper::Resource property :id, Integer, :serial => true property :gallery_id, Integer, :index => true property :title, String, :length => 1..255 property :description, Text property :type, Discriminator
belongs_to :gallery end
class Photo < Asset end
OK:
g = Gallery.get(1)
SELECT id
, user_id
, title
, published
FROM galleries
WHERE id
= 1 ORDER BY id
LIMIT 1
g.photos.first => returns nil!
SELECT id
, user_id
, gallery_id
, title
, type
FROM assets
WHERE type
IN ('Photo') AND gallery_id
IN (1) ORDER BY id
SELECT id
, user_id
, title
, published
, FROM galleries
WHERE id
= 1 ORDER BY id
LIMIT 1
SELECT id
, user_id
, gallery_id
, title
, type
FROM assets
WHERE type
IN ((1), (1), (1), (1)) AND gallery_id
IN (1) ORDER BY id
LIMIT 1
type IN ((1), (1)...) is not correct.
Thanks.
Comments and changes to this ticket
-
Mark Percival August 20th, 2008 @ 12:58 PM
I'm also coming across the bug like the one you've described. When I call the all method of a parent class in a single table inheritance I get nil's for the types on some records. It's very hard to predict when it will happen.
The other odd issue is that I can only replicate this inside of Merb. We can't seem to replicate this in specs or even in a rake task, only inside Merb IRB or Merb directly.
The workaround we're using is as follows in the parent class:
class << self alias_method :old_all, :all end def self.all(params = {}) records = [] self.descendants.each do |d| r = d.old_all(params) records += r unless r.empty? end records end
-
John Schult August 20th, 2008 @ 01:10 PM
Mark and I are going to have a look at this and see if we can't come up with a patch. Unless of course, someone can't wait to do it first :)
-
Adam French August 20th, 2008 @ 01:54 PM
your extra-bonus points (and commit rights) lay in wait ;-)
-
Mark Percival August 27th, 2008 @ 12:24 PM
- no changes were found...
-
Mark Percival August 27th, 2008 @ 12:24 PM
Here's a spec patch for dm-core that demonstrates what's going on.
The problem comes when you access a child-class property from a collection of the parent class.
We suspect it's something to do with lazy loading, and we'll start looking for a fix. In the meantime this is at least a spec that demonstrates the problem.
-
geemus (Wesley Beary) September 14th, 2008 @ 06:32 PM
- State changed from new to resolved
- Assigned user changed from Sam Smoot to geemus (Wesley Beary)
This one is/was icky. But I think I just fixed it. The spec you submitted now passes, and the STI in the merb app I was working on now cooperates as I would expect too.
I already have commit rights, so does this net me extra-extra bonus points :P
Please let me know and/or reopen the ticket if this fixes the spec and my problem, but not yours.
-
geemus (Wesley Beary) September 14th, 2008 @ 06:35 PM
Here is the commit so you can help keep me honest: http://github.com/sam/dm-core/co...
-
John Schult September 14th, 2008 @ 07:07 PM
Wow, that is SO not where we were looking. But then it was the first time I started poking through the code... We got sidetracked and could not spend any more time on it. Glad you got it sorted out Wesley :)
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »