2

내 모델; 아파트 인덱스 감안정의되지 않은 메서드`where 'for Array : 클래스

class Appartment < ActiveRecord::Base 
belongs_to :region 
end 

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

아파트 제어기 (일부)

클래스 AppartmentsController <와 ApplicationController는

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

난 올바른 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)} 

내가 오류 메시지가 "어레이에 대한 정의되지 않은 메서드` ': 클래스"내가 감사 ... 잘못 여기서 뭐하는 거지 ...

ciao..remco

+1

를 관련이없는 메모에서, 올바른 맞춤법은 "아파트"하나의 "P"입니다. – Chowlett

+0

@Chowlett 네덜란드어의 올바른 철자는 "appartement"입니다. 그러나 "meer"(더)는 정확합니다. – Substantial

+0

@gg_s - 다른 클래스 이름 ('Region','Country','House')이 영어로 나타나는 것을 제외하고는 충분히 공평합니다. – Chowlett

답변

4

레일 3의 경우 Appartment.allActiveRecord::Relation이 아닌 배열을 반환하므로 이에 대한 추가 쿼리를 수행 할 수 없습니다 (예 : where). 이 레일 4 변경할 수 있지만 평균 시간에 대신 컨트롤러에 scoped를 사용하려고합니다 :

@appartments = Appartment.scoped 
+0

감사합니다 !! 그것은 작동합니다! – Remco