开发者

Rails, modules, queries and me?

I am trying to stay DRY in my otherwise damp code and I share some address methods between various classes that use address and I am trying to put them in a module but am not confident I am doing this right. Below is what I want to do but I am unsure about two things. first, will this work using address from the class including this module and second I am unsure where to add开发者_运维百科 the ADDRESS_TYPES thingy.

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

Third, does the above code makes sense at all when included in for example an account class?


I ended up with something much simpler...

  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')    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜