Forum: Ruby on Rails "Cannot convert String to Integer" after using association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-17 01:37
So, I've written a partial that does some stuff with a given instance of
my class "entity". Entity is ActiveRecord. I'm able to retrieve all
sorts of data from an @entity, until I do something like:

<% for @attribute in @entity.attributes %>
    <div id="attribute_<%= @attribute.id %>">
    	<%= render(:partial => 'attribute') %>
    </div>
<% end %>

After which point, any attempt on my part to retrieve data from @entity
( for instance <%= @entity.id %> results in:

cannot convert String into Integer

The stack trace is:

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_proxy.rb:75:in
`[]'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_proxy.rb:75:in
`send'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_proxy.rb:75:in
`method_missing'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/has_many_association.rb:90:in
`method_missing'
(eval):1:in `id'
#{RAILS_ROOT}/app/views/main/_entity.rhtml:27
#{RAILS_ROOT}/app/views/main/index.rhtml:30
#{RAILS_ROOT}/app/views/main/index.rhtml:29:in `each'
#{RAILS_ROOT}/app/views/main/index.rhtml:29


This is really starting to tick me off. I had a workaround in which I
retrieved all the important information before I used an association,
but that won't let me retrieve more than one type of associated
information.

Help?
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2006-04-17 02:52
(Received via mailing list)
Brian,

Can you include the actual code for _entity.rhtml, given that's where
your error is happening?

Cheers,

Pete Yandell
http://9cays.com
3bef3fc4d0cbcf58f3d66c106e2fd16f?d=identicon&s=25 Mark Van Holstyn (Guest)
on 2006-04-17 03:37
(Received via mailing list)
<%= @entity.id.to_s %>
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-17 05:25
Mark Van Holstyn wrote:
> <%= @entity.id.to_s %>

Believe me, I tried that. Besides, you can't even print out the id or
any text, no matter the format.

Below is the code of _entity.rthml. The line that starts to loop for
attributes will always fail. I hope someone finds something I missed.

<div id="entity_<%= @entity.id %>"
	class="table_div"
  style="width: 100px;  height: 50px"
  table="<%= @entity.name %>"
  onClick="tableClick('entity_<%= @entity.id %>');"
>
	<div class="drag_handle"
    onMouseDown="makeDraggable('entity_<%= @entity.id %>');"
    onMouseUp="makeUnDraggable();"
  >
  	<%= @entity.name %>
  </div>

<script language="JavaScript" type="text/javascript">
<!--
	var temp = new Array();
  temp.push('<%= @entity.name %>');
  temp.push('entity_<%= @entity.id %>');
  temp.push(new Array());
	tables.push(temp);
//-->
</script>
	<div style="font-style: italic;  border-top: 1px gray solid;
border-bottom: 1px gray solid">
  	Attributes
  </div>
  <div id="entity_<%= @entity.id %>_attributes">
  	<% for @attribute in @entity.attributes %>
    <div id="attribute_<%= @attribute.id %>">
    	<%= render(:partial => 'attribute') %>
    </div>
    <% end %>
  </div>
  <div style="font-style: italic; border-top: 1px gray solid;
border-bottom: 1px gray solid">
  	Relations
  </div>
  <div id="entity__relations">
  <% for @relation in @entity.relations %>
    	<%= @relation.id %>
    <% end %>
  </div>
</div>
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2006-04-17 08:49
(Received via mailing list)
I'm betting something is broken in your attributes association. Can
you post your entity and attribute models, and the schemas for their
respective tables?
A1c4a0bb61649f18a4d65b1c7273a50f?d=identicon&s=25 Christian Klauser (sealedsun)
on 2006-04-17 12:33
Mark Van Holstyn wrote:
> <%= @entity.id.to_s %>

You could replace this line with `<%= @entity.class.name %>` and tell
us, what you get. #id is a method that returns the internal ruby id of
an object represented by an integer.

Maybe @entity is not an ActiveRecord...
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-17 15:47
Pete Yandell wrote:
> I'm betting something is broken in your attributes association. Can
> you post your entity and attribute models, and the schemas for their
> respective tables?

class Attribute < ActiveRecord::Base
	belongs_to :entity
end

class Entity < ActiveRecord::Base
	has_many :attributes
  has_many :relations,
  	:class_name => "Relation",
    :foreign_key => "entity1_id"

  has_many :incoming_relations,
  	:class_name => "Relation",
    :foreign_key => "entity2_id"
end

The schema is mundane. There's a table for each model, with ids and
other fields. The data types of all those things are correct. I checked
to make sure the key field of Entity was an integer, and it is.
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-04-17 17:51
(Received via mailing list)
Bryan Duxbury wrote:
> 	has_many :attributes
> other fields. The data types of all those things are correct. I checked
> to make sure the key field of Entity was an integer, and it is.
>

Bryan, you have chosen a name that clashes with Rails naming.

An ActiveRecord instance uses an 'attributes' hash to hold the state of
its attributes, and this has public accessors (attributes, attributes=).

regards

   Justin
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-17 19:05
Justin Forder wrote:
> Bryan Duxbury wrote:
>> 	has_many :attributes
>> other fields. The data types of all those things are correct. I checked
>> to make sure the key field of Entity was an integer, and it is.
>>
>
> Bryan, you have chosen a name that clashes with Rails naming.
>
> An ActiveRecord instance uses an 'attributes' hash to hold the state of
> its attributes, and this has public accessors (attributes, attributes=).
>
> regards
>
>    Justin

Hot damn. That would make sense, wouldn't it. Guess I'll have to rename.
Thanks.
This topic is locked and can not be replied to.