Guest User

Untitled

a guest
Feb 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. # Models
  2.  
  3. class Agent < ActiveRecord::Base
  4. belongs_to :manager
  5. belongs_to :site
  6. belongs_to :workgroup
  7. end
  8.  
  9. class Manager < ActiveRecord::Base
  10. has_many :agents
  11. end
  12.  
  13. class Site < ActiveRecord::Base
  14. has_many :agents
  15. has_many :managers, :through => :agents, :uniq => true, :order => "managers.name"
  16. has_many :workgroups, :through => :agents, :uniq => true, :order => "workgroups.name"
  17. end
  18.  
  19. class Workgroup < ActiveRecord::Base
  20. has_many :agents
  21. has_many :managers, :through => :agents, :uniq => true, :order => "managers.name"
  22. has_many :sites, :through => :agents, :uniq => true, :order => "sites.name"
  23. end
  24.  
  25. # Controller
  26.  
  27. class WorkgroupsController < ApplicationController
  28. def show
  29. @workgroup = Workgroup.find(params[:id])
  30. end
  31. end
  32.  
  33. # Existing view
  34.  
  35. <% for site in @workgroup.sites %>
  36. <h2><em><%= site.name %></em></h2>
  37. <% for manager in site.managers %>
  38. <h3><%= manager.name %></h3>
  39. <ul>
  40. <% for agent in manager.agents %>
  41. <li><%= link_to agent.name, agent_path(agent.id) %></li>
  42. <% end %>
  43. </ul>
  44. <% end %>
  45. <% end %>
  46.  
  47. # It's a scoping problem, but I just can't put my finger on it right now.
  48. # Because workgroups can have more than one site, and sites can have more than one workgroup
  49. # not to mention that managers can have agents that span workgroups, this hierarchy breaks down.
  50.  
  51. # I don't think that I want to lock it in a certain way in the associations because I need the flexibility
  52. # later to look at a group of agents by site, manager, or workgroup independently.
  53.  
  54. # New view
  55.  
  56. <% for site in @workgroup.sites %>
  57. <h2><em><%= site.name %></em></h2>
  58. <% for manager in site.managers.all(:conditions => ["agents.workgroup_id=?", @workgroup]) %>
  59. <h3><%= manager.name %></h3>
  60. <ul>
  61. <% for agent in manager.agents.all(:conditions => ["agents.workgroup_id=?", @workgroup]) %>
  62. <li><%= link_to agent.name, agent_path(agent.id) %></li>
  63. <% end %>
  64. </ul>
  65. <% end %>
  66. <% end %>
  67.  
  68. # Looks ugly. Is there a proper way to do this?
  69.  
  70. # Ultimately it should look like this:
  71. # Workgroup
  72. # Site A
  73. # Manager 1
  74. # Agent
  75. # Agent
  76. # Agent
  77. # Manager 2
  78. # Agent
  79. # Agent
  80. # Agent
  81. # Site B
  82. # Manager 3
  83. # Agent
  84. # Agent
  85. # Agent
  86. # Manager 4
  87. # Agent
  88. # Agent
  89. # Agent
Add Comment
Please, Sign In to add comment