数组的未定义方法`where':Class

时间:2013-06-06 13:11:28

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我的模特;

class Appartment < ActiveRecord::Base
 belongs_to :region
end

class Region < ActiveRecord::Base
 belongs_to :country
 has_many :appartments
 has_many :houses
end

公寓控制员(部分)

class AppartmentsController&lt; ApplicationController中

def index
 add_breadcrumb "homepage", :root_path
 @country = Country.find(params[:country_id])
 @regions = @country.regions
 @appartments = Appartment.all
end

在appartment索引视图中,我确实创建了一个每个循环来获取正确的url链接。

.span9
  %article.chat.t_xs
    .article-base
      #container
        - @regions.each do |region|
          - @appartments.where(region_id: region.id).each do |appartment|
            .item{:class => appartment.features_to_html_class }

              %section
                .span4
                  %h2 #{link_to appartment.name, country_region_appartment_path(@country, region, appartment)}
                  %ul.stars.floatstars
                    %li.yellowstars{:style => "width: #{appartment.avg_rating * 25}px !important;"}
                  %footer
                    %br
                    %p 
                      = raw truncate(appartment.property_description, :length => 250, :omission => '...')
                      #{link_to "meer", country_region_appartment_path(@country, region, appartment)}

我收到错误消息“未定义的方法`在哪里'对于Array:Class”我在这里做错了什么...谢谢......

ciao..remco

1 个答案:

答案 0 :(得分:4)

在Rails 3中,Appartment.all返回一个数组,而不是ActiveRecord::Relation,因此您无法对其执行其他查询(如where)。这将在Rails 4中更改,但与此同时,请尝试在控制器中使用scoped

@appartments = Appartment.scoped