Hi,

I'm pretty new with Ruby and have been struggling for a while how to
make work  a join between tables on a non-standard schema. I know the
proper way of doing it would be migrating the schema and using the
ActiveRecord convention but as long as I'm using this to consume
existing data for testing purposes, that's not an option.

Tables:
- AGREEMENTS
- AGREEMENT_TYPES

Both tables contain a column 'AGREEMENT_TYPE_ID' that could be used for
the join. It is also the primary key for the 'AGREEMENT_TYPES' table.

This is the models definition:

class Agreement < ActiveRecord::Base
  self.table_name = 'AGREEMENTS'
  self.primary_key = 'AGREEMENT_ID'
  has_one :AgreementType, :primary_key => 'AGREEMENT_TYPE_ID',
:foreign_key => 'AGREEMENT_TYPE_ID'
end

class AgreementType < ActiveRecord::Base
  self.table_name = 'AGREEMENT_TYPES'
  self.primary_key = 'AGREEMENT_TYPE_ID'
  belongs_to :Agreement, :primary_key => 'AGREEMENT_TYPE_ID',
:foreign_key => 'AGREEMENT_TYPE_ID'
end


What I wanted to achive is to consume a field called 'name' from the
AGREEMENT_TYPES table. This is the instantiation:

puts Agreements.joins(:Agreement_Types).explain

EXPLAIN for: SELECT "AGREEMENTS".* FROM "AGREEMENTS" INNER JOIN
"AGREEMENT_TYPES" ON "AGREEMENT_TYPES"."AGREEMENT_TYPE_ID" =
"AGREEMENTS"."AGREEMENT_ID"

Using include also seems to list only the AGREMENTS fields:

@agr = Agreement.find(:all, :include => :AgreementType)

And it does not let me access the 'name' field:

irb(main):081:0> puts @agr.name
NoMethodError: undefined method `name' for #<Array:0x1cfb258>
        from (irb):81
        from C:/tools/Ruby193/bin/irb:12:in `<main>'

Any help will be appreaciated.

Thanks.

-- 
Posted via http://www.ruby-forum.com/.