所以我是 RoR 的新手,我似乎无法进行选择声明。这些是以下类:
模型:
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
并具有以下 View :
<ul class="lists">
<%= render @lists%>
</ul>
然后出现如下错误:
ActiveRecord::StatementInvalid in Lists#index_where
SQLite3::SQLException: no such column: category.id: SELECT "lists".* FROM "lists" WHERE (category.id = 2) ORDER BY lists.created_at DESC
我做错了什么?谢谢并致以最诚挚的问候。
最佳答案
@lists = Category.find(2).lists
关于ruby-on-rails - Ruby On Rails - 无法通过报复在 has_many 中执行 where 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16261626/