Skip to content

Instantly share code, notes, and snippets.

@knowtheory
Created April 26, 2009 05:12
Show Gist options
  • Select an option

  • Save knowtheory/101890 to your computer and use it in GitHub Desktop.

Select an option

Save knowtheory/101890 to your computer and use it in GitHub Desktop.
gem 'dm-core', '0.10'
require 'dm-core'
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
letters = %w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
letters.each_with_index do |letter,index|
belong_classes, has_classes = letters.partition{ |other_letter| letters.rindex(other_letter) <= letters.rindex(letter) }
belong_string = belong_classes.map do |c|
"\tbelongs_to :#{c.downcase}" unless c == letter
end.join("\n")
has_string = has_classes.map do |c|
through_class = letters[letters.rindex(c)-1] # previous class
str = "\thas n, :#{c.downcase}"
str << ", :through => :#{through_class.downcase}" if letters.rindex(c) > (letters.rindex(letter)+1)
str
end.join("\n")
thing = <<-CLASS
class #{letter}
include DataMapper::Resource
property :id, Serial
#{belong_string}
#{has_string}
end
CLASS
puts thing
eval(thing)
end
DataMapper.auto_migrate!
a = A.create
a.q.create # => totally freaking works!
a.s.create # => this is the last point at which it works
a.t.create # => fail
a.z.create # => explodes, stack trace too deep!
>> a.t.create # => fail
SystemStackError: stack level too deep
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
... 2934 levels...
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `map'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:68:in `links'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:114:in `query'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:42:in `query_for'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:62:in `collection_for'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:146:in `lazy_load'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:76:in `get'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/one_to_many.rb:116:in `t'
from (irb):36>>
?> a = A.create
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.005485) INSERT INTO "as" DEFAULT VALUES
=> #<A @id=1>
>> a.q.create # => totally freaking works!
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000095) INSERT INTO "bs" ("a_id") VALUES (1)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000063) INSERT INTO "cs" ("b_id", "a_id") VALUES (1, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000106) INSERT INTO "ds" ("c_id", "a_id", "b_id") VALUES (1, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000105) INSERT INTO "es" ("d_id", "a_id", "b_id", "c_id") VALUES (1, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000083) INSERT INTO "fs" ("a_id", "b_id", "c_id", "d_id", "e_id") VALUES (NULL, NULL, NULL, NULL, 1)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000092) INSERT INTO "gs" ("a_id", "b_id", "c_id", "d_id", "e_id", "f_id") VALUES (NULL, NULL, NULL, NULL, NULL, 1)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000119) INSERT INTO "hs" ("a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, 1)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000133) INSERT INTO "is" ("h_id", "a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id") VALUES (1, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000170) INSERT INTO "js" ("a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000115) INSERT INTO "ks" ("a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000144) INSERT INTO "ls" ("k_id", "a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000138) INSERT INTO "ms" ("k_id", "l_id", "a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000174) INSERT INTO "ns" ("m_id", "k_id", "l_id", "a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000160) INSERT INTO "os" ("n_id", "k_id", "l_id", "a_id", "m_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000132) INSERT INTO "ps" ("k_id", "l_id", "a_id", "m_id", "b_id", "n_id", "c_id", "o_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000131) INSERT INTO "qs" ("p_id", "k_id", "l_id", "a_id", "m_id", "b_id", "n_id", "c_id", "o_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
=> #<Q @id=1 @p_id=1 @k_id=nil @l_id=nil @a_id=nil @m_id=nil @b_id=nil @n_id=nil @c_id=nil @o_id=nil @d_id=nil @e_id=nil @f_id=nil @g_id=nil @h_id=nil @i_id=nil @j_id=nil>
>> a.s.create # => this is the last point that works
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000093) INSERT INTO "bs" ("a_id") VALUES (1)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000056) INSERT INTO "cs" ("b_id", "a_id") VALUES (2, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000254) INSERT INTO "ds" ("c_id", "a_id", "b_id") VALUES (2, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.001423) INSERT INTO "es" ("d_id", "a_id", "b_id", "c_id") VALUES (2, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000189) INSERT INTO "fs" ("a_id", "b_id", "c_id", "d_id", "e_id") VALUES (NULL, NULL, NULL, NULL, 2)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000111) INSERT INTO "gs" ("a_id", "b_id", "c_id", "d_id", "e_id", "f_id") VALUES (NULL, NULL, NULL, NULL, NULL, 2)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000101) INSERT INTO "hs" ("a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, 2)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000139) INSERT INTO "is" ("h_id", "a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id") VALUES (2, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000318) INSERT INTO "js" ("a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000171) INSERT INTO "ks" ("a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000129) INSERT INTO "ls" ("k_id", "a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000234) INSERT INTO "ms" ("k_id", "l_id", "a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (NULL, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000158) INSERT INTO "ns" ("m_id", "k_id", "l_id", "a_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000174) INSERT INTO "os" ("n_id", "k_id", "l_id", "a_id", "m_id", "b_id", "c_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000179) INSERT INTO "ps" ("k_id", "l_id", "a_id", "m_id", "b_id", "n_id", "c_id", "o_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000145) INSERT INTO "qs" ("p_id", "k_id", "l_id", "a_id", "m_id", "b_id", "n_id", "c_id", "o_id", "d_id", "e_id", "f_id", "g_id", "h_id", "i_id", "j_id") VALUES (2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000170) INSERT INTO "rs" ("k_id", "l_id", "a_id", "m_id", "b_id", "n_id", "c_id", "o_id", "d_id", "p_id", "e_id", "q_id", "f_id", "g_id", "r_id", "h_id", "i_id", "j_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2, NULL, NULL, NULL, NULL, NULL, NULL)
Sun, 26 Apr 2009 07:22:02 GMT ~ debug ~ (0.000204) INSERT INTO "rs" ("k_id", "l_id", "a_id", "m_id", "b_id", "n_id", "c_id", "o_id", "d_id", "p_id", "e_id", "q_id", "f_id", "g_id", "r_id", "h_id", "i_id", "j_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL)
=> #<R @id=2 @k_id=nil @l_id=nil @a_id=nil @m_id=nil @b_id=nil @n_id=nil @c_id=nil @o_id=nil @d_id=nil @p_id=nil @e_id=nil @q_id=nil @f_id=nil @g_id=nil @r_id=1 @h_id=nil @i_id=nil @j_id=nil>
>>
?> A.all.q
Sun, 26 Apr 2009 07:22:20 GMT ~ debug ~ (0.000064) SELECT "id" FROM "as" ORDER BY "id"
Sun, 26 Apr 2009 07:22:20 GMT ~ debug ~ (0.001067) SELECT "qs"."id", "qs"."p_id", "qs"."k_id", "qs"."l_id", "qs"."a_id", "qs"."m_id", "qs"."b_id", "qs"."n_id", "qs"."c_id", "qs"."o_id", "qs"."d_id", "qs"."e_id", "qs"."f_id", "qs"."g_id", "qs"."h_id", "qs"."i_id", "qs"."j_id" FROM "qs" INNER JOIN "ps" ON "qs"."p_id" = "ps"."id" INNER JOIN "os" ON "ps"."o_id" = "os"."id" INNER JOIN "ns" ON "os"."n_id" = "ns"."id" INNER JOIN "ms" ON "ns"."m_id" = "ms"."id" INNER JOIN "ls" ON "ms"."l_id" = "ls"."id" INNER JOIN "ks" ON "ls"."k_id" = "ks"."id" INNER JOIN "js" ON "ks"."j_id" = "js"."id" INNER JOIN "is" ON "js"."i_id" = "is"."id" INNER JOIN "hs" ON "is"."h_id" = "hs"."id" INNER JOIN "gs" ON "hs"."g_id" = "gs"."id" INNER JOIN "fs" ON "gs"."f_id" = "fs"."id" INNER JOIN "es" ON "fs"."e_id" = "es"."id" INNER JOIN "ds" ON "es"."d_id" = "ds"."id" INNER JOIN "cs" ON "ds"."c_id" = "cs"."id" INNER JOIN "bs" ON "cs"."b_id" = "bs"."id" INNER JOIN "as" ON "bs"."a_id" = "as"."id" WHERE "ps"."id" = 1 GROUP BY "qs"."id", "qs"."p_id", "qs"."k_id", "qs"."l_id", "qs"."a_id", "qs"."m_id", "qs"."b_id", "qs"."n_id", "qs"."c_id", "qs"."o_id", "qs"."d_id", "qs"."e_id", "qs"."f_id", "qs"."g_id", "qs"."h_id", "qs"."i_id", "qs"."j_id" ORDER BY "qs"."id"
=> [#<Q @id=1 @p_id=1 @k_id=nil @l_id=nil @a_id=nil @m_id=nil @b_id=nil @n_id=nil @c_id=nil @o_id=nil @d_id=nil @e_id=nil @f_id=nil @g_id=nil @h_id=nil @i_id=nil @j_id=nil>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment