Skip to content

Instantly share code, notes, and snippets.

@madebydna
Created March 17, 2011 18:16
Show Gist options
  • Save madebydna/874829 to your computer and use it in GitHub Desktop.
Save madebydna/874829 to your computer and use it in GitHub Desktop.
class Address < ActiveRecord::Base
DELTA = 0.001
validates :line1, :city, :presence => true
belongs_to :country
belongs_to :organisation
belongs_to :person
belongs_to :address_type
belongs_to :service
# more code
end
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
class Organization < ActiveRecord::Base
has_many :addresses, :as => :addressable
# ...
end
class Organisation < ActiveRecord::Base
def registered_address
self.addresses.each do |addr|
if addr.address_type_id == 1
return addr
end
end
nil
end
# could be written simply
def registered_address
addresses.where(:address_type_id => 1).first
end
end
def find_phone(phone_number, phone_type_id)
phones.where(:number => phone_number, :phone_type_id => phone_type_id).first
# self.phones.each do |phone|
# if phone.number == phone_number and phone.phone_type_id == phone_type_id
# return phone
# end
# end
# nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment