2

我的模型;

class Appartment < ActiveRecord::Base
 belongs_to :region
end

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

公寓控制器(部分)

类 AppartmentsController < 应用控制器

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

在公寓索引视图中,我确实创建了一个 each 循环来获取正确的 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)}

我收到错误消息“数组:类的未定义方法`where'”我在这里做错了什么...谢谢...

ciao..remco

4

1 回答 1

4

在 Rails 3 中,Appartment.all返回一个数组,而不是一个ActiveRecord::Relation,所以你不能对它执行额外的查询(比如where)。这将在 Rails 4 中发生变化,但与此同时,请尝试scoped在您的控制器中使用:

@appartments = Appartment.scoped
于 2013-06-06T13:13:40.473 回答