Undefined method ' each_with_index'

Hi,

I’m creating a new model bt
class BT < ActiveRecord::Base

has_many :bt_items
belongs_to :user

end

and gather BT in a controller like this

@bts = BT.find_by_product_id(@product.id)

then render in a view like this

<%=render :partial => "bt/bt", :collection => @bts %>

and I got the following error

undefined method `each_with_index’ for #BT:0x45ed70c

Any setting I should add to the BT model to get the method work?

THanks

The line:

@bts = BT.find_by_product_id(@product.id)

is only returning a single BT. You’re trying to render as if you had a
collection of BTs, thus:

@bts = BT.find_all_by_product_id(@product.id)

should fix your problem.

Jason

Hi Bontina,

Bontina C. wrote:

@bts = BT.find_by_product_id(@product.id)

<%=render :partial => “bt/bt”, :collection => @bts %>

undefined method `each_with_index’ for #BT:0x45ed70c

Just change the find to:

@bts = BT.find_all_by_product_id(@product.id)

Best regards,
Bill

What are you trying to do within the ‘bt/bt’ partial? What are you
trying to iterate over? If you simply want access to a BT object in
your partial you could try <%= render :partial => “bt/bt”, :object =>
@bts %>

Jim