2011-02-08 2 views
0

나는 습기가 많은 코드에 건조한 상태를 유지하려고 노력하고 있으며 주소를 사용하는 다양한 클래스 사이에서 일부 주소 메서드를 공유하고이를 모듈에 넣으려고하고 있지만 자신감이 없다. 아래는 내가하고 싶은 것이지만 두 가지에 대해서는 확신이 없습니다. 먼저,이 모듈을 포함하여 클래스의 주소를 사용하여 작동하고 두 번째로 ADDRESS_TYPES를 추가 할 위치를 확신 할 수 없습니다.레일, 모듈, 쿼리 및나요?

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')  
관련 문제