Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 数组的未定义方法“where”:类_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 数组的未定义方法“where”:类

Ruby on rails 数组的未定义方法“where”:类,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,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 在appartment索引视图中,我创建了一个each循环来获取正确的url链接 .span9 %article.chat.t_xs .a

我的模型

class Appartment < ActiveRecord::Base
 belongs_to :region
end

class Region < ActiveRecord::Base
 belongs_to :country
 has_many :appartments
 has_many :houses
end
在appartment索引视图中,我创建了一个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)}
我收到错误消息undefined method'where'for Array:Class我在这里做错了什么…谢谢

ciao..remco

在Rails 3中,Appartment.all返回一个数组,而不是ActiveRecord::Relation,因此不能像where那样对其执行其他查询。这将在Rails 4中发生变化,但同时,请尝试在控制器中使用scoped:

@appartments = Appartment.scoped
在Rails 3中,Appartment.all返回一个数组,而不是ActiveRecord::Relation,因此不能像where那样对其执行其他查询。这将在Rails 4中发生变化,但同时,请尝试在控制器中使用scoped:

@appartments = Appartment.scoped

另一方面,正确的拼写是公寓,荷兰语中的p@Chowlett,正确的拼写是appartement。但是米尔·莫尔是正确的。@gg_s-很公平,除了其他类名Region、Country、House似乎是英语。在一个无关的注释中,正确的拼写是公寓,一个p。@Chowlett在荷兰语中,正确的拼写是appartement。但是米尔·莫尔是正确的。@ggs-很公平,除了其他类名Region、Country、House似乎是英语。