1

所以我是 RoR 的新手,我似乎无法选择 where 语句。这些是以下类:

型号:

class List < ActiveRecord::Base  
  has_many :list_categorization
  has_many :category, :through => :list_categorization
end

class ListCategorization < ActiveRecord::Base
  attr_accessible :category_id, :list_id

  belongs_to :category
  belongs_to :list
end

class Category < ActiveRecord::Base
  attr_accessible :name

  has_many :list_categorizations
  has_many :lists, :through => :list_categorizations
end

我想要做的是选择一个给定类别的列表。我尝试在 list_controler 中使用以下代码对此进行简化:

class ListsController < ApplicationController

    @lists = List.where("category.id = ?", 2)
  end
end

并具有以下观点:

<ul class="lists">
  <%= render @lists%>
</ul>

然后出现以下错误:

列表中的 ActiveRecord::StatementInvalid#index_where

SQLite3::SQLException: 没有这样的列: category.id: SELECT "lists".* FROM "lists" WHERE (category.id = 2) ORDER BY lists.created_at DESC

我究竟做错了什么?谢谢和最好的问候。

4

2 回答 2

2

我认为你试图做这样的事情:

@lists = List.joins(:categories).where('category.id = 2')

你可以这样做:

@lists = Category.find(2).lists
于 2013-04-28T10:02:23.800 回答
2
@lists = Category.find(2).lists
于 2013-04-28T10:02:27.637 回答