# Associations troubles

**URL:** <https://www.ruby-forum.com/t/associations-troubles/115217>\
**Category:** Rails\
**Created:** [September 21, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/associations-troubles/115217 "2007-09-21T20:54:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 21, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/associations-troubles/115217/1 "2007-09-21T20:54:41Z")

</div>

Still working this global legacy database. I’m autogenerating the  
base model files.

Here’s the error:

ActionView::TemplateError (compile error  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/base.rb:1359: syntax error, unexpected tINTEGER  
Object::3  
^) on line #21 of app/views/planner/show.rhtml:  
18:

## Tests:
  
19: 
```
20: <% a =@subproject.tests-%>
21: <%= a.class %>
22:
23:
```

C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/base.rb:1358:in `compute_type' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1066:in`instantiate\_without\_callbacks’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/callbacks.rb:204:in `instantiate' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:427:in`find\_by\_sql’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/base.rb:427:in `collect!' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:427:in`find\_by\_sql’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/base.rb:997:in `find_every' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:418:in`find’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/associations/has\_many\_association.rb:91:in `find' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/associations/association_collection.rb:159:in`find\_target’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/associations/has\_many\_association.rb:123:in  
`load_target' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/associations/association_proxy.rb:122:in`method\_missing’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
active\_record/associations/has\_many\_association.rb:98:in  
`method_missing' #{RAILS_ROOT}/app/views/planner/show.rhtml:21:in`\_run\_rhtml\_47app47views47planner47show46rhtml’

Relevant file snippets:

app/controllers/planner\_controller.rb:  
class PlannerController \< ApplicationController

session :session\_key =\> ‘\_capacity\_planner\_id’

SQL\_SELECT\_FROM\_BASE = “SELECT  
rt.valueStr AS region,  
p.Name AS projectName,  
at.valueStr AS area,  
sp.\*  
FROM  
dbo.projectAttr AS at  
INNER JOIN  
dbo.project AS p ON at.projectId = p.projectId  
INNER JOIN  
dbo.subProject AS sp ON p.projectId = sp.projectId  
INNER JOIN  
dbo.subProjectAttr AS rt ON sp.subProjectId = rt.subProjectId  
WHERE  
(at.attributeId = ‘{F5AF5E6C-380C-499D-83ED-FA4BA6AD4D6B}’) AND  
(rt.attributeId = ‘{1E9A75EA-BBD4-479E-A1B3-1CA30D891371}’)”

def show  
@subproject = AgSubProject.find\_by\_sql(SQL\_SELECT\_FROM\_BASE + “AND  
(sp.subProjectId = ‘#{params[:id]}’)”)[0]  
end

end

models/ag\_sub\_project.rb:  
class AgSubProject \< ActiveRecord::Base  
set\_table\_name “subProject”  
set\_primary\_key “subProjectId”  
has\_many :folders, :class\_name =\>  
“AgFolder”, :foreign\_key =\> “subProjectId”  
belongs\_to :entityStatus, :class\_name =\>  
“AgEntityStatu”, :foreign\_key =\> :entityStatusId  
belongs\_to :project, :class\_name =\>  
“AgProject”, :foreign\_key =\> :projectId  
has\_many :subProjectAttrs, :class\_name =\>  
“AgSubProjectAttr”, :foreign\_key =\> “subProjectId”  
has\_many :subProjectInvGroups, :class\_name =\>  
“AgSubProjectInvGroup”, :foreign\_key =\> “subProjectId”  
has\_many :subProjectInvModels, :class\_name =\>  
“AgSubProjectInvModel”, :foreign\_key =\> “subProjectId”  
has\_many :tests, :class\_name =\>  
“AgTest”, :foreign\_key =\> “subProjectId”

def after\_create  
self[:subProjectId] = new\_guid  
end

has\_and\_belongs\_to\_many(:db\_attributes, :join\_table =\>  
“subProjectAttr”,  
:class\_name =\> “AgAttribute”, :foreign\_key =\> :subProjectId,  
:association\_foreign\_key =\> :attributeId)  
end

models/ag\_test.rb:  
class AgTest \< ActiveRecord::Base  
set\_table\_name “test”  
set\_primary\_key “testId”  
belongs\_to :subProject, :class\_name =\>  
“AgSubProject”, :foreign\_key =\> :subProjectId  
def after\_create  
self[:testId] = new\_guid  
end  
end

====

The error message is the first interesting thing in the log file. In  
an effort to narrow the problem, I’ve commented out all of the  
associations for ag\_test except the one listed.

Note that I can gather @subproject.tests, but I cannot even take the  
class of the resultant object. The error Object::3 has also been seen  
with Object::1.

???

Thanks,

Student

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 21, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/associations-troubles/115217/2 "2007-09-21T20:56:41Z")

</div>

One detail: The Object::3 output is actually a debug reference I  
added to expose modularized\_name in the call.

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 21, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/associations-troubles/115217/3 "2007-09-21T21:36:47Z")

</div>

I get the same sort of error if I replace show from planner\_controller  
with the following:

def show  
@tests = AgTest.find(:all, :conditions =\> “subProjectID =  
‘#{params[:id]}’”)  
STDERR.puts #{@tests.class}  
end

So we can leave the viewer out of the discussion. We can probably  
leave the association thing out as well.

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 24, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/associations-troubles/115217/4 "2007-09-24T15:56:25Z")

</div>

I had not touched the source when this began. I checked the install,  
it is clean.  
The added line was a STDERR.puts which did not affect the block  
output. As for the syntax error, it’s coming from a class\_eval  
statement…

```
    # Returns the class type of the record using the current

```

module as a prefix. So descendents of  
# MyApp::Business::Account would appear as  
MyApp::Business::AccountSubclass.  
def compute\_type(type\_name)  
modularized\_name = type\_name\_with\_module(type\_name)  
begin  
 → class\_eval(modularized\_name, **FILE** , **LINE** )  
rescue NameError  
class\_eval(type\_name, **FILE** , **LINE** )  
end  
end

I’ll unzip a fresh copy of InstaRails & see what happens…

On Sep 21, 2:55 pm, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 24, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/associations-troubles/115217/5 "2007-09-24T16:10:11Z")

</div>

Do you have a type column on one of these tables? I’m guessing you do  
and that it contains integers. When active record sees a column called  
type it assumes you are using single table inheritance and that the  
value of the type column is the name of the class for the row in  
question. 3 isn’t a valid class name hence the error.

You can use set\_inheritance\_column to tell AR to use a different column

Fred

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 24, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/associations-troubles/115217/6 "2007-09-24T16:37:34Z")

</div>

Score. I just set the inheritance column to xxx and we’re looking  
good.

On Sep 24, 9:10 am, Frederick C. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![student](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@student](https://www.ruby-forum.com/u/student)\
**Post date:** [September 21, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/associations-troubles/115217/7 "2007-09-21T21:55:45Z")

</div>

Student wrote:

> C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
> active\_record/base.rb:1359: syntax error, unexpected tINTEGER

You’re getting a syntax error out of activerecord?

It sounds like you’ve accidentally broken the source.  
Try uninstalling the gem  
and reinstalling?
