Note that there are some explanatory texts on larger screens.

plurals
  1. POget all products of category and child categories (rails, awesome_nested_set)
    text
    copied!<p>having an e-commerce application under development i am trying to get my head around the following problem: I have my categories realized through the awesome_nested_set plugin. If I list my articles through selecting one category everything works fine, but for some links I want to show all the products of one category and the products of its child categories.</p> <p>here is the controller code that works fine with only one category:</p> <pre><code> # products_controller.rb def index if params[:category] @category = Category.find(params[:category]) #@products = @category.product_list @products = @category.products else @category = false @products = Product.scoped end @products = @products.where("title like ?", "%" + params[:title] + "%") if params[:title] @products = @products.order("created_at).page(params[:page]).per( params[:per_page] ? params[:per_page] : 25) @categories = Category.all end </code></pre> <p>The line I commented out is a helper method I wrote myself in the category model which returns all products of the category and its child categories in an array.</p> <p>It is defined as followed:</p> <pre><code># app/models/category.rb def product_list self_and_ancestors.to_a.collect! { |x| x.products } end </code></pre> <p>Now when I uncomment this line and try to select one category my products controller code breaks with errors like</p> <pre><code>undefined method `order' for #&lt;Array:0x1887c2c&gt; </code></pre> <p>or </p> <pre><code>undefined method `page' for #&lt;Array:0x1887c2c&gt; </code></pre> <p>because I am using ordering and pagination and it can't order the arary anymore.</p> <p>Any ideas how to get all the products in an ActiveRecord Relation element in my controller? thanks</p> <p><strong>UPDATE</strong></p> <p>so, when I use the following:</p> <pre><code>class Category &lt; ActiveRecord::Base acts_as_nested_set attr_accessible :name, :description, :lft, :rgt, :parent_id has_many :categorizations has_many :products, :through =&gt; :categorizations attr_accessor :product_list def branch_ids self_and_descendants.map(&amp;:id).uniq end def all_products Product.find(:all, :conditions =&gt; { :category_id =&gt; branch_ids } ) end end </code></pre> <p>and ask the controller for <code>@category.all_products</code> i get the following error:</p> <pre><code>Mysql::Error: Unknown column 'products.category_id' in 'where clause': SELECT `products`.* FROM `products` WHERE `products`.`category_id` IN (6, 8, 9) </code></pre> <p>How would I get all products with this constellation?</p> <p><strong>UPDATE 2</strong></p> <p>Ok, so I am going to start a bounty. </p> <p>If I try:</p> <p>def all_products Categorization.find(:all, :conditions => { :category_id => branch_ids } ) end</p> <p>I get again <code>undefined method</code>order' for #` I need to know how I can get all the products of a many_to_many relation as an ActiveRecord relation.</p> <p><strong>UPDATE 3</strong></p> <p>I put the relevant code in a gist <a href="https://gist.github.com/1211231" rel="noreferrer">https://gist.github.com/1211231</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload