ruby-on-rails - 数组 :Class 的未定义方法 `where'

标签 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

公寓 Controller (部分)

类 AppartmentsController

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

在公寓索引 View 中,我创建了一个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’”我在这里做错了什么...谢谢...

再见..雷姆科

最佳答案

在 Rails 3 中,Appartment.all 返回一个数组,而不是 ActiveRecord::Relation,因此您无法对其执行其他查询(例如 哪里)。这将在 Rails 4 中发生变化,但同时,请尝试在 Controller 中使用 scoped 来代替:

@appartments = Appartment.scoped

关于ruby-on-rails - 数组 :Class 的未定义方法 `where',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16963031/

相关文章:

ruby-on-rails - IndexTank 如何处理权限

mysql - 带有 mysql 问题的 Rails 3

ruby-on-rails - 在 Rails 3.1 中更改 created_at 的格式?

ruby-on-rails - before_save 期间的方法错误

ruby-on-rails - 在 rails 中为 options_for_select 定义 id 或 class 的方法?

ruby-on-rails - Hanami 有 .present 的替代品吗?

ruby-on-rails - ruby rails : permission denied when using "rails generate controller welcome"

jquery - Jquery 中的循环不工作

ruby-on-rails - strip_tags 助手,标签/标签,除了 ruby​​ on rails 3

ruby-on-rails - 创建资源后如何重置我选择的选择菜单?