简体   繁体   English

Rails 3-搜索,HABTM和类别化

[英]Rails 3 - Search, HABTM and Caterogizations

I'm using the Advanced Search Form from RailsCasts (#111), but I want to add the possibity to search in subcategories with a collection check boxes. 我正在使用RailsCasts(#111)中的“高级搜索表单”,但是我想添加可能性以使用集合复选框在子类别中进行搜索。

class Category < ActiveRecord::Base
  # post_id, food_id, pet_id
class Food < ActiveRecord::Base
  # id, name
class Pet < ActiveRecord::Base
  # id, name
class Post < ActiveRecord::Base
  # id, title.

In my search.rb model, I tried : 在我的search.rb模型中,我尝试了:

posts = posts.where(food_ids: food_ids) if food_ids.present?

I created a new model called : Subcategorysearch.rb, exactly the same as Categoty.rb, but it only for the search request. 我创建了一个新模型:Subcategorysearch.rb,与Categoty.rb完全相同,但是它仅用于搜索请求。 Inside I have: post_id, food_id, pet_id. 里面我有:post_id,food_id,pet_id。

With or without the new model, I always get : 无论有没有新模型,我总能得到:

ActiveRecord::StatementInvalid in Searches#show SQLite3::SQLException: no such column: posts.food_ids ActiveRecord :: StatementInvalid在Searches#show SQLite3 :: SQLException中:无此类列:posts.food_ids

Thanks by advance. 预先感谢。

EDIT : 编辑

When I create a new post, the Subcategories aren't going in the Post table, but in the Category table. 创建新帖子时,“子类别”不在“帖子”表中,而是在“类别”表中。 I need to make the Post search working with the possibility of filtering with a collection check boxes the subcategories. 我需要使Post搜索工作,并可能将带有集合复选框的过滤作为子类别。

EDIT 2 编辑2

I tried a lot a things, but I think I'm kind of lost. 我做了很多尝试,但我觉得我有些失落。

The "real" problem, is to perform a search engine like the Railscasts #111 - Advanced Search Form and using something like the Railscasts #17 - HABTML Checkboxes, with the "categorizations". “真正的”问题是执行带有“分类”的搜索引擎,如Railscasts#111-高级搜索表单,并使用诸如Railscasts#17-HABTML复选框之类的东西。

The final result I need, is, for exemple: Search a Post and be able to filter the result with checkboxes collection : 例如,我需要的最终结果是:搜索帖子,并能够使用复选框集合过滤结果:

Post tile = My first post
Food = Name 1
Pet = Name 2

... ...

And then, only show the Posts that contains the Title like "My first post", Food like "Name 1" or by ID, and the same for Pet. 然后,仅显示包含标题(如“我的第一条帖子”),食物(如“名称1”或ID)和“宠物”的帖子。

Again, thank you by advance for your support. 再次感谢您的支持。

You are using food_ids instead of food_id in your where clause key. 您在where子句键中使用food_ids而不是food_id As you can understand from the error, posts table does not have a food_ids column, it has a food_id column. 从错误中您可以理解,posts表没有food_ids列,而是有food_id列。

Try: 尝试:

posts = posts.where(food_id: food_ids) if food_ids.present?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM