"Cannot convert String to Integer" after using association


#1

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:insend’
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:inmethod_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:ineach’
#{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?


#2

Brian,

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

Cheers,

Pete Y.
http://9cays.com


#3

<%= @entity.id.to_s %>


#4

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?


#5

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

#6

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…


#7

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.


#8

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


#9

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.