0

当然,这是一个简单的问题,但我不能抱怨

class Service < ActiveRecord::Base
   has_many :offers
   has_many :offices,:through=> :offers
   has_many :addresses,:through => :offices
   has_many :locations,:through => :adreesses,:include => :offices
end

class Offer < ActiveRecord::Base
   belongs_to :service
   belongs_to :office
end

class Office < ActiveRecord::Base
   has_many :offers
   has_many :services,:through => :offers
   has_one :adreess
end

class Address
   belongs_to :office
   belongs_to :location
end

class Location < ActiveRecord::Base
   has_many :addressess
   has_many :offices,:through => :addresses,:uniq=>true
end

我想显示,例如:

Service name: Entrega especial

  Location name: Distrito Capital
     Offices:
         Name: San Martin

  Location name: Carabobo
     Offices:
         Name: San diego

但是当我这样做时

service = Service.where(:nombre=> "Entrega especial")
locations = service.locations

locations.offices.each do |o|
   puts o.nombre # only I want the offices with that service
in that Location, no all offices in that location
end

我无法意识到如何只显示关系中的办公室,而不是该位置中的所有办公室,即使没有该服务提供,以一种不合时宜的方式,不使用关联扩展,并管理 proxy_xxx 对象

4

1 回答 1

0

也许最好通过办公室协会

@offices = service.offices.includes(:location)

@offices.group_by(&:location).each do |location, offices_by_location|
  puts "Location: #{location}"
  offices_by_location.each do |office|
    puts "Office: #{office}"
  end
end
于 2013-02-08T14:59:07.067 回答