Reputation: 42182
I'm trying to use a relationship between tables, the primary key in both tables has the same name adm_id (i know, i know, but have no control over the db) I use activerecord without Rails in JRuby.
When i get the related records (the emails) i get the error below. Could somoene help me out ?
require 'java'
require 'activerecord-jdbc-adapter'
require "C:/jruby-1.7.4/bin/ojdbc14.jar"
Java::OracleJdbcDriver::OracleDriver
ActiveRecord::Base.establish_connection(
:adapter => 'jdbc',
:driver => 'oracle.jdbc.driver.OracleDriver',
:url => 'jdbc:oracle:thin:@xxxxxx:xxxx:xxx',
:username=>'xxx',
:password=>'xxx'
)
class Adm < ActiveRecord::Base
self.table_name = 'scheme1.db_all'
self.primary_key = 'adm_id'
has_many :emails
end
class Email < ActiveRecord::Base
self.table_name = 'scheme2.db_email'
self.primary_key = 'adm_id'
belongs_to :adm, :foreign_key => 'adm_id'
end
lid = Adm.where(adm_id: 99999999).take(1) #ok
email = Email.where(adm_id: 99999999).take(1) #ok
p lid.emails
#error: NoMethodError: undefined method `emails' for #<Array:0x19bc716>
Upvotes: 1
Views: 224
Reputation: 16226
If you just want a single record, don't pass any arguments to take
:
lid = Adm.where(adm_id: 3441029).take
email = Email.where(adm_id: 3441029).take
p lid.emails
If any argument is supplied to take
, even if it's 1, an array of results is returned.
Upvotes: 2