范围不在关系中

时间:2014-09-02 14:31:15

标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-3.1

我的模特:

class House
belongs_to: country
scope :published, -> { where(published: true) }
end

class Country
has_many: houses
end

我想展示出版的房屋和来自x国的房屋;

house_controller:

@country = Country.friendly.find(params[:country_id])
@houses = @country.houses.published.order(:sorting)

我收到错误“undefined method` published”

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您可以使用已发布的范围,如下所示:

House.where(country_id: @country.id).published.order(:sorting)