Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. require 'dm-core'
  2. require 'dm-migrations'
  3. require 'dm-sqlite-adapter'
  4.  
  5. DataMapper.setup :default, "sqlite::memory:"
  6.  
  7. class Comment
  8. include DataMapper::Resource
  9. property :id, Serial
  10. belongs_to :post
  11. end
  12.  
  13. class Post
  14. include DataMapper::Resource
  15. property :id, Serial
  16. has n, :comments
  17. belongs_to :user
  18. end
  19.  
  20. class User
  21. include DataMapper::Resource
  22. property :id, Serial
  23. has n, :posts
  24. end
  25.  
  26. DataMapper.auto_migrate!
  27.  
  28. user = User.create
  29. 3.times { Post.create(user: user) }
  30.  
  31. repository do
  32. posts = Post.all.entries
  33.  
  34. DataObjects::Sqlite3.logger = DataMapper::Logger.new($stdout, :debug)
  35. posts.each { |post| post.comments.entries }
  36. end
  37.  
  38. # ~ (0.000040) SELECT "id", "post_id" FROM "comments" WHERE "post_id" = 1 ORDER BY "id"
  39. # ~ (0.000039) SELECT "id", "post_id" FROM "comments" WHERE "post_id" = 2 ORDER BY "id"
  40. # ~ (0.000031) SELECT "id", "post_id" FROM "comments" WHERE "post_id" = 3 ORDER BY "id"
Add Comment
Please, Sign In to add comment