Could Some one please explain to me what is going on here?
This is an example that I put together to show y'all whats up:
class Person
include DataMapper::Resource
property :id, Serial
property :type, Discriminator
property :name, String
property :age, Integer
end
class Male < Person
end
class Father < Male
property :job, String
end
class Son < Male
end
class Female < Person
end
class Mother < Female
property :favorite_song, String
end
class Daughter < Female
end
DataMapper.auto_upgrade!
If I call Person.all
I get:
Person.all
=> [#<Son @id=1 @type=Son @name="Mike" @age=23 @status=nil>,
#<Son @id=2 @type=Son @name="Carlos" @age=12 @status=nil>,
#<Father @id=3 @type=Father @name="Robert" @age=55 @job=<not loaded>>,
#<Mother @id=4 @type=Mother @name="Wanda" @age=47 @status=nil @favorite_song=<not loaded>>,
#<Daughter @id=5 @type=Daughter @name="Meg" @age=16 @status=nil>]
And if I call Person.get(3).type
I get:
Person.get(3).type
=> Father
And Male.all
gives me this:
Male.all
=> [#<Son @id=1 @type=Son @name="Mike" @age=23 @status=nil>,
#<Son @id=2 @type=Son @name="Carlos" @age=12 @status=nil>,
#<Father @id=3 @type=Father @name="Robert" @age=55 @job=<not loaded>>]
And Male.get(3).type
gives this:
Male.get(3).type
=> Father
But Person.all(:type => Male)
returns an empty array: (?)
Person.all(:type => Male)
=> []
However, Person.all(:type => Son)
returns all of the Son
type entries (=/)
Person.all(:type => Son)
=> [#<Son @id=1 @type=Son @name="Mike" @age=23 @status=nil>,
#<Son @id=2 @type=Son @name="Carlos" @age=12 @status=nil>]
If I try do do something like @person = People.all
I get all entries in @person exactly as you would expect. but I cannot do something like @men = @person.all(:type => Male)
I get an empty array.
@men = @people.all( :type => Male)
=> []
I would be using this with Sinatra. What I would like is to be able to for example grab all of my people in one gram from the DB and hold them in @people
but still be able to sort/filter them for various uses in my views. I have done something similar with associations before and it worked rather well. But I would like to try STI because it seems to be a more concise way to deal with my data.
I have noticed things as well that if I do something like @women = Female.all
I get all of the woman, but if I do something like @woman.each do |woman|
I am unable to access contain properties in my views (i.e. woman.favorite_song
returns nothing).
Am I missing something about how this works? I do not understand what is going on here at all and any help would be appreciated.