# "Cannot convert String to Integer" after using association

**URL:** <https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953>\
**Category:** Rails\
**Created:** [April 17, 2006, 1:37am UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953 "2006-04-17T01:37:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 1:37am UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/1 "2006-04-17T01:37:35Z")

</div>

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 %\>

  
\<%= render(:partial =\> ‘attribute’) %\>  

  
\<% 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?

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 2:52am UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/2 "2006-04-17T02:52:31Z")

</div>

Brian,

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

Cheers,

Pete Y.  
[http://9cays.com](http://9cays.com)

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 3:37am UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/3 "2006-04-17T03:37:57Z")

</div>

\<%= @entity.id.to\_s %\>

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 8:49am UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/4 "2006-04-17T08:49:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 5:25am UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/5 "2006-04-17T05:25:09Z")

</div>

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.

 \<%= @entity.name %\> 

```
<div style="font-style: italic; border-top: 1px gray solid; 

```

border-bottom: 1px gray solid"\>  
Attributes

 \<% for @attribute in @entity.attributes %\> 
 \<%= render(:partial =\> 'attribute') %\> 
 \<% end %\> 

 Relations 

 \<% for @relation in @entity.relations %\> \<%= @relation.id %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 12:33pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/6 "2006-04-17T12:33:54Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 3:47pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/7 "2006-04-17T15:47:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/8 "2006-04-17T17:51:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [April 17, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-to-integer-after-using-association/57953/9 "2006-04-17T19:05:54Z")

</div>

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.
