Created
May 12, 2011 20:19
-
-
Save emmanuel/969353 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Parent | |
| include DataMapper::Resource | |
| def self.default_repository_name | |
| :parent_repo | |
| end | |
| property :id, Serial | |
| has n, :children, :model => "StiBase" | |
| # My problem: is there a more efficient way to accomplish this? | |
| def grandchildren | |
| mixed_list = [] | |
| ::StiBase.descendants.map do |sti_model| | |
| mixed_list.concat sti_model.all(:parent => self).associated.to_a | |
| end | |
| mixed_list | |
| end | |
| end | |
| class StiBase | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :type, Discriminator | |
| property :parent_id, Integer, :index => :parent | |
| belongs_to :parent | |
| end | |
| class StiChildOne < StiBase | |
| has 1, :associated, :model => "StiChildOneAssociated" | |
| end | |
| class StiChildTwo < StiBase | |
| has 1, :associated, :model => "StiChildTwoAssociated" | |
| end | |
| class StiChildOneAssociated | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :associated_id, Integer, :index => :associated | |
| belongs_to :child, :child_key => :associated_id, :model => "StiChildOne" | |
| end | |
| class StiChildTwoAssociated | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :associated_id, Integer, :index => :associated | |
| belongs_to :child, :child_key => :associated_id, :model => "StiChildTwo" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "rubygems" | |
| require "dm-core" | |
| require "dm-migrations" | |
| DataMapper::Logger.new($stdout, :debug) | |
| DataMapper.setup(:parent_repo, "sqlite3::memory:") | |
| DataMapper.setup(:default, "sqlite3::memory:") | |
| require "problem_setup" | |
| DataMapper.auto_migrate! | |
| p = Parent.create | |
| a1 = StiChildOneAssociated.create | |
| a2 = StiChildTwoAssociated.create | |
| c1 = StiChildOne.create(:parent => p, :associated => a1) | |
| c2 = StiChildTwo.create(:parent => p, :associated => a2) | |
| # what I'd really like to do is `p.children.associated`, | |
| puts p.children.associated.inspect rescue nil | |
| # but I figure that's not on the table, so I fall back to trying: | |
| collection_1 = StiChildOne.all(:parent => p).associated | |
| p collection_1 # => [#<StiChildOneAssociated @id=1>] | |
| collection_2 = StiChildTwo.all(:parent => p).associated | |
| p collection_2 # => [#<StiChildTwoAssociated @id=1>] | |
| # Ideal would be a way to traverse from parent through children to the | |
| # different types of :associated (polymorphic) in one query. | |
| # Short of that, is there a most efficient way to accomplish this than | |
| # Parent#grandchildren? | |
| p p.grandchildren |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment