Forum: Ruby on Rails undefined method ' each_with_index'

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bontina C. (Guest)
on 2007-02-15 14:27

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

  has_many :bt_items
  belongs_to :user


and gather BT in a controller like this

  @bts = BT.find_by_product_id(

then render in a view like this

  <div id= "bt">
<%=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?

Jason R. (Guest)
on 2007-02-15 15:48
(Received via mailing list)
The line:

@bts = BT.find_by_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(

should fix your problem.

Bill W. (Guest)
on 2007-02-15 15:51
(Received via mailing list)
Hi Bontina,

Bontina C. wrote:

>  @bts = BT.find_by_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(

Best regards,
Jimtron (Guest)
on 2007-02-16 03:09
(Received via mailing list)
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 %>

This topic is locked and can not be replied to.