Created
March 17, 2011 18:16
-
-
Save madebydna/874829 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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