RubyOnRails

系统教程 行业动态 更新时间:2024-06-14 17:02:18
RubyOnRails - 无法通过关联在has_many中执行where语句(RubyOnRails - Can't do a where statment in a has_many through relatiation)

所以我是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

并通过以下观点:

<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

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

So I'm new to RoR and i can't seem to do a select where statment. These are the following classes:

The models:

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

And what i'm trying to do is to select a list with a given category. I tried to do a simplified version of this in the list_controler with the following code:

class ListsController < ApplicationController @lists = List.where("category.id = ?", 2) end end

And with the following view:

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

Then the following error apears:

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

What am I doing wrong? Thanks and best regards.

最满意答案

@lists = Category.find(2).lists @lists = Category.find(2).lists

更多推荐

本文发布于:2023-04-21 18:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/32a7475fc39147230c12c942996ad399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RubyOnRails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!