Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Organization
- belongs_to :location
- has_and_belongs_to_many :events
- end
- class Location
- belongs_to :city
- has_many :organizations
- end
- class Event
- belongs_to :city
- has_and_belongs_to_many :organizations
- end
- class City
- has_many :locations
- has_many :events
- after_save :set_org_ids
- def set_org_ids
- from_location = Organization.joins(:location).where(:locations => {:city_id => id})
- from_events = Organization.joins(:events).where(:events => {:city_id => id})
- # you must create an org_ids column on cities table before this will work
- self.org_ids = (from_location | from_events).collect(&:id).join(',')
- self.save
- end
- def organizations
- Organization.find(self.org_ids)
- end
- end
Add Comment
Please, Sign In to add comment