使用 Rails 3.2。我想了解如何编写正确的递归循环。这是关联和控制器:
# country.rb
class Country < ActiveRecord::Base
has_many :states
end
# state.rb
class State < ActiveRecord::Base
belongs_to :country
has_many :zones
has_many :cities, :through => :zones
end
# zone.rb
class Zone < ActiveRecord::Base
belongs_to :state
belongs_to :city
end
# city.rb
class City < ActiveRecord::Base
has_many :photos, :as => :attachable
end
# photo.rb
class Photo < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
has_attached_file :data, :options
end
# countries_controller.rb
class CountriesController < ApplicationController
def show
@country = Country.find(params[:id], :includes => [:states => [:cities => :photos]])
@photos = @country.country_photos
end
end
我将在下面编写一个愚蠢的递归循环来解释我想要实现的目标:从城市获取照片:
# countries/show.html.erb
<%= @country.country_photos.inspect # just to test %>
# country.rb
class Country < ActiveRecord::Base
def country_photos
all_photos = []
self.states.each do |state|
state.cities.each do |city|
city.photos.each do |photo|
all_photos << photo
end
end
end
end
end
# Expected output: [photo_object_1, photo_object_2]
我试过map
在country_photos
:
if (photos = state.map(&:cities).flatten.map(&:photos).flatten)
photos
end
但它有性能问题:执行 400 毫秒。
编写递归循环的正确方法是什么?如果给出了逐步的解释,请欣赏。谢谢。