Rails 3查询多维数组

时间:2012-03-15 14:37:08

标签: ruby-on-rails ruby arrays ruby-on-rails-3 multidimensional-array

我有一个名为@venues的数组,其中包含半径范围内的所有餐馆。每个地点has_many菜肴,我想在venue中插入一系列这些菜肴。

@venues = Venue.within(radius, :origin => [lat, lng]).order('distance ASC')

@venues.each do |venue|
  dishes = venue.dishes.where("? BETWEEN DATE(served_from) AND DATE(served_until)", Date.today)
  # insert dishes into venue
end

如何将菜肴插入场地,以便我可以访问场地的所有可用菜肴:@venues[i].dishes

1 个答案:

答案 0 :(得分:4)

Dish本身添加范围可能更有意义:

scope :today, :conditions => ["? BETWEEN DATE(served_from) AND DATE(served_until)", Date.today]

有了这个,您可以使用dishes

访问可用的@venues[i].dishes.today