Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. @clients = Client.find_all_by_company_id(current_user.company_id)
  2. @clients.each do |client|
  3.  
  4. project = Project.find_all_by_client_id(client.id)
  5. puts project.name
  6.  
  7. project.each do |project|
  8.  
  9. task = Task.find_all_by_project_id(project.id)
  10. puts task.name
  11.  
  12. end
  13. end
  14.  
  15. Category.includes(:posts => [{:comments => :guest}, :tags]).find(1)
  16.  
  17. require 'active_record'
  18. require 'logger'
  19.  
  20. # ===== Config =====
  21. ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
  22. ActiveRecord::Base.logger = Logger.new $stdout
  23. ActiveSupport::LogSubscriber.colorize_logging = false
  24.  
  25. # ===== Schema =====
  26. ActiveRecord::Schema.define do
  27. self.verbose = false
  28.  
  29. create_table :clients do |t|
  30. t.string :name
  31. t.integer :company_id
  32. end
  33.  
  34. create_table :companies do |t|
  35. t.string :name
  36. end
  37.  
  38. create_table :projects do |t|
  39. t.string :name
  40. t.integer :client_id
  41. end
  42.  
  43. create_table :tasks do |t|
  44. t.string :name
  45. t.integer :project_id
  46. end
  47.  
  48. create_table :minutes do |t|
  49. t.integer :quantity
  50. t.integer :task_id
  51. end
  52. end
  53.  
  54. # ===== Classes =====
  55. class Company < ActiveRecord::Base
  56. has_many :clients
  57. end
  58.  
  59. class Client < ActiveRecord::Base
  60. belongs_to :company
  61. has_many :projects
  62. end
  63.  
  64. class Project < ActiveRecord::Base
  65. belongs_to :client
  66. has_many :tasks
  67. end
  68.  
  69. class Task < ActiveRecord::Base
  70. belongs_to :project
  71. has_many :minutes
  72. end
  73.  
  74. class Minute < ActiveRecord::Base
  75. belongs_to :task
  76. end
  77.  
  78. # ===== Data =====
  79. Company.create! name: 'Activision' do |company|
  80. company.clients.build name: 'Robert Kotick' do |client|
  81. client.projects.build name: 'Website Redesign' do |project|
  82. project.tasks.build name: 'Development' do |task|
  83. task.minutes.build quantity: 100
  84. end
  85. end
  86. end
  87. end
  88.  
  89. # ===== Querying and displaying =====
  90. company = Company.find_by_name 'Activision'
  91. clients = Client.includes(projects: {tasks: :minutes}).where(company_id: company.id)
  92.  
  93. print "n----- The query makes four requests, regardless of how much data you have. -----nn"
  94. clients.inspect # do this to force loading since AR queries are lazy
  95.  
  96. print "n----- some representation of the data (notice no queries while iterating through) -----nn"
  97. clients.each do |client|
  98. puts client.name
  99. client.projects.each do |project|
  100. puts "-- #{project.name}"
  101. project.tasks.each do |task|
  102. puts "--- #{task.name}"
  103. task.minutes.each do |minute|
  104. puts "---- #{minute.quantity}"
  105. end
  106. end
  107. end
  108. end
  109.  
  110. # ===== Output =====
  111.  
  112. # >> D, [2012-09-12T00:01:42.755414 #72855] DEBUG -- : (0.7ms) select sqlite_version(*)
  113. # >> D, [2012-09-12T00:01:42.755890 #72855] DEBUG -- : (0.2ms) CREATE TABLE "clients" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "company_id" integer)
  114. # >> D, [2012-09-12T00:01:42.756327 #72855] DEBUG -- : (0.1ms) CREATE TABLE "companies" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255))
  115. # >> D, [2012-09-12T00:01:42.756728 #72855] DEBUG -- : (0.1ms) CREATE TABLE "projects" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "client_id" integer)
  116. # >> D, [2012-09-12T00:01:42.757122 #72855] DEBUG -- : (0.1ms) CREATE TABLE "tasks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "project_id" integer)
  117. # >> D, [2012-09-12T00:01:42.757531 #72855] DEBUG -- : (0.1ms) CREATE TABLE "minutes" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "quantity" integer, "task_id" integer)
  118. # >> D, [2012-09-12T00:01:42.906877 #72855] DEBUG -- : (0.0ms) begin transaction
  119. # >> D, [2012-09-12T00:01:42.909242 #72855] DEBUG -- : SQL (0.5ms) INSERT INTO "companies" ("name") VALUES (?) [["name", "Activision"]]
  120. # >> D, [2012-09-12T00:01:42.934937 #72855] DEBUG -- : SQL (24.7ms) INSERT INTO "clients" ("company_id", "name") VALUES (?, ?) [["company_id", 1], ["name", "Robert Kotick"]]
  121. # >> D, [2012-09-12T00:01:42.936110 #72855] DEBUG -- : SQL (0.1ms) INSERT INTO "projects" ("client_id", "name") VALUES (?, ?) [["client_id", 1], ["name", "Website Redesign"]]
  122. # >> D, [2012-09-12T00:01:42.937001 #72855] DEBUG -- : SQL (0.1ms) INSERT INTO "tasks" ("name", "project_id") VALUES (?, ?) [["name", "Development"], ["project_id", 1]]
  123. # >> D, [2012-09-12T00:01:42.937767 #72855] DEBUG -- : SQL (0.1ms) INSERT INTO "minutes" ("quantity", "task_id") VALUES (?, ?) [["quantity", 100], ["task_id", 1]]
  124. # >> D, [2012-09-12T00:01:42.938005 #72855] DEBUG -- : (0.0ms) commit transaction
  125. # >> D, [2012-09-12T00:01:42.939882 #72855] DEBUG -- : Company Load (0.1ms) SELECT "companies".* FROM "companies" WHERE "companies"."name" = 'Activision' LIMIT 1
  126. # >>
  127. # >> ----- The query makes four requests, regardless of how much data you have. -----
  128. # >>
  129. # >> D, [2012-09-12T00:01:42.940458 #72855] DEBUG -- : Client Load (0.1ms) SELECT "clients".* FROM "clients" WHERE "clients"."company_id" = 1
  130. # >> D, [2012-09-12T00:01:42.943272 #72855] DEBUG -- : Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."client_id" IN (1)
  131. # >> D, [2012-09-12T00:01:42.943919 #72855] DEBUG -- : Task Load (0.1ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."project_id" IN (1)
  132. # >> D, [2012-09-12T00:01:42.944520 #72855] DEBUG -- : Minute Load (0.1ms) SELECT "minutes".* FROM "minutes" WHERE "minutes"."task_id" IN (1)
  133. # >>
  134. # >> ----- some representation of the data (notice no queries while iterating through) -----
  135. # >>
  136. # >> Robert Kotick
  137. # >> -- Website Redesign
  138. # >> --- Development
  139. # >> ---- 100
  140.  
  141. @clients = current_user.company.clients
  142. @clients.each do |client|
  143. projects = client.projects
  144.  
  145. # puts project.name # makes no sense here
  146.  
  147. projects.each do |project|
  148.  
  149. project.tasks.each do |task|
  150. puts task.name
  151. end
  152. end
  153. end
  154.  
  155. Client.includes(
  156. :company =>{:projects=>:tasks})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement