Associations troubles

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: <H2>Tests:</H2> 19: <pre> 20: <% a = @subproject.tests -%> 21: <%= a.class %> 22: 23: </pre>

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

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

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.

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

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