Topic: problems using acts_as_tree and HABTM together

I've started builing just looked at acts_as_tree for a category tree structure for browse the store. I managed to successfully create a collapsable menu using this which is all good. Now to link products to categories using the HABTM relationship.

This is where I have come unstuck, what should be seemingly easy has 'I think' been complicated by acts_as_tree since I want to view the products in a particular category but no matter what category id I do a find on products for I end up with a full list of products for that category and all others. I imagine that when for products for a particular category the query then recurses the category tree, up and down and returns all products. How annoying?

Has anyone else seen this and offer any suggestions as how to acheive what I want to do? I guess ideally there would be a way of limiting the depth of the recursion.

Re: problems using acts_as_tree and HABTM together

I don't think it's recursing.

Can you post your Category and Product Model, and the code your using for the find?

Re: problems using acts_as_tree and HABTM together


class Product < ActiveRecord::Base

  validates_presence_of :title, :retail_price
  validates_numericality_of :cost_price, :retail_price

  has_and_belongs_to_many :materials
  has_and_belongs_to_many :product_option_values
  has_and_belongs_to_many :product_options
  has_many :product_images
  has_many :cart_items

  has_and_belongs_to_many :categories



class Category < ActiveRecord::Base

  has_and_belongs_to_many :products


I have used the following commented out methods of trying to get the right results and all seem to give the same problem. I am relatively new to rails so I may have missed something obvious but as far as I am aware '@categories.products' should hold the list of products for the particular category I am looking at.


  def category
    @category = Category.find(@params[:id])
    @products = @category.products
    #@products = Product.find(:all, :conditions => [ "category_id = ?", @params[:id] ], :include => 'categories')
    #@products = Product.find(:all, :conditions => [ "category_id = ?", ], :include => 'categories')
    #@product_pages, @products = paginate :products, :conditions => [ '', ], :order => '', :per_page => 10
    flash[:notice] = "Category does not exist."
    redirect_to(:controller => 'store', :action => 'category', :id => 1)

Re: problems using acts_as_tree and HABTM together

Did you check the categories_products table to make sure there aren't actually records associating every product to every category?