2011-02-08 27 views
0

我想在我的其他潮溼的代碼中保持DRY,並且在使用地址的各種類之間共享一些地址方法,並且我試圖將它們放在一個模塊中,但我不確定我是否正確執行此操作。以下是我想要做的,但我不確定兩件事情。首先,這個工作是否會使用包含這個模塊的類的地址,其次我不確定在哪裏添加ADDRESS_TYPES。Rails,模塊,查詢和我?

module AddressModule 
    class << self 
    def delivery_address 
     where(address_type: ADDRESS_TYPES.delivery) 
    end 

    def billing_address 
     where(address_type: ADDRESS_TYPES.billing) 
    end 
    end 
end 

第三,當包含在例如帳戶類中時,上述代碼是否有意義?

回答

0

我結束了一些簡單得多......

has_many :addresses, :as => :addressable, :dependent => :destroy 

    has_one :billing_address, :as => :addressable 
    validates :billing_address, presence:true 
    accepts_nested_attributes_for :billing_address, 
    :allow_destroy => true, 
    :reject_if  => missing_attrs?('street_one', 'zip', 'city', 'country_id') 

    has_one :delivery_address, :as => :addressable 
    validates :delivery_address, presence:true 
    accepts_nested_attributes_for :delivery_address, 
    :allow_destroy => true, 
    :reject_if  => missing_attrs?('street_one', 'zip', 'city', 'country_id')