#1238 new
ravinggenius

Discriminator issue

Reported by ravinggenius | April 8th, 2010 @ 09:47 PM

I have a Rails 3 app, and I'm having a problem querying the base class with a Discriminator property. For instance:

class Person
  include DataMapper::Resource

  property :id, Serial
  property :discriminator, Discriminator, :required => true
end

class Male < Person; end
class Female < Person; end

class Man < Male; end
class Boy < Male; end

class Woman < Female; end
class Girl < Female; end

http://datamapper.org/docs/misc states that Discriminator supports deep inheritance, yet...

Male.all == (Man.all || Boy.all) # expected, total win
Person.all <> (Male.all || Female.all) # FAIL, why?

Person.all returns an empty array. I triple checked to make sure there is actual data in the database (there is), and in irb, I get the same results. Am I doing something wrong, or is this a bug?

Comments and changes to this ticket

  • ravinggenius

    ravinggenius April 19th, 2010 @ 03:11 PM

    • Tag changed from discriminator, rails3 to dm-discriminator, rails3

    bump...

    Am I the only one with this issue?

  • sundbp
  • Dawid Marcin Grzesiak

    Dawid Marcin Grzesiak April 20th, 2010 @ 08:29 AM

    Why do you use "&&" operator. Shouldn't it be "+" or "||"?

  • ravinggenius

    ravinggenius April 20th, 2010 @ 08:31 AM

    Updated syntax... sorry about that.

  • Dawid Marcin Grzesiak

    Dawid Marcin Grzesiak April 20th, 2010 @ 08:42 AM

    It seems like query builder takes care of default scope just for the first model...

    require 'rubygems'
    require 'datamapper'
    
    DataMapper::Logger.new(STDOUT, :debug)
    DataMapper.setup(:default, 'sqlite3::memory:')
    
    class Person
      include DataMapper::Resource
      property :id, Serial
      property :discriminator, Discriminator
    end
    
    class Male < Person; end
    class Female < Person; end
    
    class Man < Male; end
    class Boy < Male; end
    
    class Woman < Female; end
    class Girl < Female; end
    
    DataMapper.auto_migrate!
    
    (Man.all || Boy.all).to_a
    #SELECT "id", "discriminator" FROM "people" WHERE "discriminator" IN ('Man') ORDER BY "id"
    
    (Male.all || Female.all).to_a
    #SELECT "id", "discriminator" FROM "people" WHERE "discriminator" IN ('Male', 'Man', 'Boy') ORDER BY "id"
    
  • Dawid Marcin Grzesiak

    Dawid Marcin Grzesiak April 20th, 2010 @ 08:46 AM

    You can workaround the issue like this:

    Person.all(:discriminator => [Male, Female]).to_a
    

    which results in this query:

    SELECT "id", "discriminator" FROM "people" WHERE ("discriminator" IN ('Male', 'Female') AND "discriminator" IN ('Person', 'Male', 'Female', 'Man', 'Boy', 'Woman', 'Girl')) ORDER BY "id"
    
  • ravinggenius

    ravinggenius April 20th, 2010 @ 04:09 PM

    I tried that, and this is what was generated:

    SELECT "id", "discriminator"
    FROM "people"
    WHERE ("discriminator" IN ('NodeCollection') AND "discriminator" IN ('Male', 'Female'))
    ORDER BY "id"
    
  • ravinggenius

    ravinggenius April 30th, 2010 @ 05:28 PM

    I was working on another project for work using Rails 3 and DataMapper. (The project was initially setup very similar to this one.) We were forced to switch that project over to ActiveRecord because DataMapper didn't seem to play nice with Rails 3 in development mode. Somehow the models where not be reloaded properly. I think that may be related to the issue here.

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Pages