I am trying to retrieve root category - subcategories by using Ancestry gem. I tried this in rails console it worked. It retrieved sub id's without problem, but when I am using in controller( actual application) it shows such error :
Mysql2::Error: Operand should contain 1 column(s): SELECT COUNT(*) FROM `categories` WHERE (id = 2,7,15,16)
Code in controller :
@category = Category.find_by_name(params[:category])
@sub_ids = @category.child_ids
@subcat = Category.where("id = ?", @sub_ids)
Thanks (((((:
It's quite clear that the category with provided name doesn't exist (so Category.find_by_name
method returns nil
).
You probably don't set @params
instance variable properly.
About your second error, the last line in your code should be:
@subcat = Category.where(id: @sub_ids)
Or, to make it simpler, you could use existing library to handle this kind of logic, like acts_as_tree.