I'm struggling to make a join between tables work 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 data for testing purposes, that's not an option.
The join can be made using the key 'AGREEMENT_TYPE_ID' that exists in both tables.
I have the following in the models definition:
class Agreement < ActiveRecord::Base self.table_name = 'AGREEMENTS' self.primary_key = 'AGREEMENT_ID' has_one :AgreementType, :foreign_key => 'AGREEMENT_TYPE_ID' end class AgreementType < ActiveRecord::Base self.table_name = 'AGREEMENT_TYPES' self.primary_key = 'AGREEMENT_TYPE_ID' belongs_to :Agreement, :foreign_key => 'AGREEMENT_TYPE_ID' end
I'm trying to access the contents of the field 'name' from the 'AGREEMENT_TYPE_ID' table as follows:
irb(main):081:0> @output = Agreement.first.AgreementType.name NoMethodError: undefined method `name' for nil:NilClass from (irb):81 from C:/tools/Ruby193/bin/irb:12:in `<main>'