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.
Bryan D. (Guest)
on 2006-04-17 03: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?
Pete Y. (Guest)
on 2006-04-17 04: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 Y.
http://9cays.com
Mark Van H. (Guest)
on 2006-04-17 05:37
(Received via mailing list)
<%= @entity.id.to_s %>
Bryan D. (Guest)
on 2006-04-17 07:25
Mark Van H. 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>
Pete Y. (Guest)
on 2006-04-17 10: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?
Christian K. (Guest)
on 2006-04-17 14:33
Mark Van H. 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...
Bryan D. (Guest)
on 2006-04-17 17:47
Pete Y. 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.
Justin F. (Guest)
on 2006-04-17 19:51
(Received via mailing list)
Bryan D. 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
Bryan D. (Guest)
on 2006-04-17 21:05
Justin F. wrote:
> Bryan D. 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.