what am i doing wrong here?
[ahoward@localhost rest]$ cat schema.sql
create table people (
id serial,
first_name text,
last_name text
);
create table students (
id serial,
person_id int
);
create table teachers (
id serial,
person_id int
);
create table courses (
id serial,
subject text
);
create table courses_students (
id serial,
course_id int,
student_id int
);
create table courses_teachers (
id serial,
course_id int,
teacher_id int
);
[ahoward@localhost rest]$ cat app/models/{p,s,t,c}*
class Person < ActiveRecord::Base
has_one :student
has_one :teacher
end
class Student < ActiveRecord::Base
belongs_to :person
end
class Teacher < ActiveRecord::Base
belongs_to :person
end
class Course < ActiveRecord::Base
has_many :course_student
has_many :course_teacher
end
class CourseStudent < ActiveRecord::Base
set_table_name "courses_students"
has_and_belongs_to_many :course, :join_table => table_name
has_and_belongs_to_many :student, :join_table => table_name
end
class CourseTeacher < ActiveRecord::Base
set_table_name "courses_teachers"
has_and_belongs_to_many :course, :join_table => table_name
has_and_belongs_to_many :teacher, :join_table => table_name
end
[ahoward@localhost rest]$ ./script/console
Loading development environment.
>> ct = CourseTeacher::find 1
=> #<CourseTeacher:0xb781129c @attributes={"id"=>"1",
“teacher_id”=>“1”, “course_id”=>“1”}>
>> c = Course::find 1
=> #<Course:0xb780e8bc @attributes={“subject”=>“latin”, “id”=>“1”}>
>> c.course_teacher
=> [#<CourseTeacher:0xb780bf04 @attributes={“id”=>“1”,
“teacher_id”=>“1”, “course_id”=>“1”}>, #<CourseTeacher:0xb780bec8
@attributes={“id”=>“2”, “teacher_id”=>“2”, “course_id”=>“1”}>]
>> ct.course
ActiveRecord::StatementInvalid: ERROR: column
courses_teachers.course_teacher_id does not exist
: SELECT * FROM courses LEFT JOIN courses_teachers ON courses.id =
courses_teachers.course_id WHERE (courses_teachers.course_teacher_id = 1
)
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/connection_adapters/abstract_adapter.rb:67:in
log' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/connection_adapters/postgresql_adapter.rb:113:in
execute’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/connection_adapters/postgresql_adapter.rb:290:in
select' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/connection_adapters/postgresql_adapter.rb:94:in
select_all’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:447:in
find_by_sql' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:411:in
find’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/has_and_belongs_to_many_association.rb:58:in
find' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/has_and_belongs_to_many_association.rb:93:in
find_target’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/association_proxy.rb:81:in
load_target' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/association_proxy.rb:74:in
method_missing’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/has_and_belongs_to_many_association.rb:81:in
method_missing' from /usr/local/lib/ruby/1.8/irb.rb:296:in
output_value’
from /usr/local/lib/ruby/1.8/irb.rb:149:in eval_input' from /usr/local/lib/ruby/1.8/irb.rb:145:in
signal_status’
from /usr/local/lib/ruby/1.8/irb.rb:145:in eval_input' from /usr/local/lib/ruby/1.8/irb.rb:144:in
each_top_level_statement’
from /usr/local/lib/ruby/1.8/irb.rb:144:in eval_input' from /usr/local/lib/ruby/1.8/irb.rb:70:in
start’
from /usr/local/lib/ruby/1.8/irb.rb:69:in catch' from /usr/local/lib/ruby/1.8/irb.rb:69:in
start’
from /usr/local/bin/irb:13>>
why does ActiveRecord seem to think the pk is
‘courses_teachers.course_teacher_id’ and not ‘courses_teachers.id’ ??
considering i told it nothing other than the table_name why should it
think
the pk does not follow normal convention?
also, why on earth would the join table not be assumed to be the
table_name in
general? am i missing something critical here?
kind regards.