3

I have this in my controller:

class User::ResourcesController < User::UserController
  def index
     @resource_type = ResourceType.find_by_name(params[:resource_type].to_s.titleize)
     @products = @products.includes(:resources).where(:resources => { :resource_type_id => @resource_type.id })

     respond_to do |format|
       format.html # index.html.erb
       format.xml  { render :xml => @resources }
     end
  end
end

I am trying to get my resources to be filtered so in my view i can use the code below and have it only pull the resources that have the correct resource_type_id.

@products.each do |product|
  product.resources.count
end
Joshua Novak
  • 144
  • 1
  • 10

2 Answers2

3
@products = Product.includes(:resources).where("resources.resource_type_id = ?", @resource_type.id)
Adam Albrecht
  • 6,680
  • 4
  • 31
  • 35
  • oops I just realized that you have @products = @products.inc.... (and I repeated it). Where is the original @products variable coming from? Try my edited answer above. – Adam Albrecht Mar 31 '11 at 17:03
1

Try something like this:

@products = Product.includes(:resources).where(resources: { 
  resource_type_id: @resource_type.id 
})

It's safest and easier to read.

BryanH
  • 5,826
  • 3
  • 34
  • 47
Oswaldo Ferreira
  • 1,339
  • 16
  • 15