I am attempting to setup a belongs_to / has_many relationship
and the resulting where clause is wrong:
ruby code
require "rubygems"
require_gem "activerecord"
require "pp"
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "devbox..internal",
:database => "test2",
:username => "XXX",
:password => "XXXpassword" )
class Job < ActiveRecord::Base
# doesnt have id as the primary key
set_primary_key "jobid"
has_many :audits
end
class Audit < ActiveRecord::Base
set_table_name "job_audit" # legacy name
belongs_to :job,
:class_name => "Job",
:foreign_key => "jobid"
end
jj = Job.find(900000)
pp jj
jj.audits.each do |ja|
pp ja
end
what I get is this
ttyp6 > ruby report.rb
#<Job:0x810d760
@attributes=
{"doa"=>"0",
"item_id"=>"0",
"faultid"=>"3110",
"status"=>"CLOSED",
"proj_mustfinish"=>"0000-00-00 00:00:00",
... omitted columns ...
"jobid"=>"900000",
"partid"=>"0",
"resp_ts"=>"10:42:00",
... omitted columns ...
"escalation"=>"0"}>
which is the correct job record
and then this
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/connection_adapters/abstract_adapter.rb:128:in `log':
Mysql::Error: Unknown column 'job_audit.job_id' in 'where clause':
SELECT * FROM job_audit WHERE (job_audit.job_id = 900000)
(ActiveRecord::StatementInvalid)
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/connection_adapters/mysql_adapter.rb:243:in `execute'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/connection_adapters/mysql_adapter.rb:395:in `select'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/connection_adapters/abstract/database_statements.rb:7:in
`select_all'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/base.rb:424:in `find_by_sql'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/base.rb:994:in `find_every'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/base.rb:415:in `find'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/associations/has_many_association.rb:91:in `find'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/associations/association_collection.rb:159:in
`find_target'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/associations/has_many_association.rb:123:in
`load_target'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/associations/association_proxy.rb:122:in
`method_missing'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/
active_record/associations/has_many_association.rb:98:in
`method_missing'
from report.rb:35
From my understanding of the belongs_to / has_many options
and the legacy renaming of the id field the where clause above
should be
SELECT * FROM job_audit WHERE (job_audit.jobid = 900000)
not
SELECT * FROM job_audit WHERE (job_audit.job_id = 900000)
what am i doing wrong?
mjt