my models;
class Appartment < ActiveRecord::Base
belongs_to :region
end
class Region < ActiveRecord::Base
belongs_to :country
has_many :appartments
has_many :houses
end
appartment controller(part)
class AppartmentsController < ApplicationController
def index
add_breadcrumb "homepage", :root_path
@country = Country.find(params[:country_id])
@regions = @country.regions
@appartments = Appartment.all
end
In the appartment index view i do created a each loop to get the correct url links.
.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)}
i get the error message "undefined method `where' for Array:Class" What am i doing wrong here...thanks...
ciao..remco
In Rails 3, Appartment.all
returns an array, not an ActiveRecord::Relation
, so you can't perform additional queries on it (like where
). This will change in Rails 4, but in the mean time, try using scoped
in your controller instead:
@appartments = Appartment.scoped
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.