Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. class Organization
  2. belongs_to :location
  3. has_and_belongs_to_many :events
  4. end
  5.  
  6. class Location
  7. belongs_to :city
  8. has_many :organizations
  9. end
  10.  
  11. class Event
  12. belongs_to :city
  13. has_and_belongs_to_many :organizations
  14. end
  15.  
  16. class City
  17. has_many :locations
  18. has_many :events
  19.  
  20. after_save :set_org_ids
  21.  
  22. def set_org_ids
  23. from_location = Organization.joins(:location).where(:locations => {:city_id => id})
  24. from_events = Organization.joins(:events).where(:events => {:city_id => id})
  25. # you must create an org_ids column on cities table before this will work
  26. self.org_ids = (from_location | from_events).collect(&:id).join(',')
  27. self.save
  28. end
  29.  
  30. def organizations
  31. Organization.find(self.org_ids)
  32. end
  33.  
  34. end
Add Comment
Please, Sign In to add comment