I am trying to do something right from Agile Web Dev with Rails book
and it does not work. I must be missing something obvious.
I have a one-to-many relationship:
class Draft < ActiveRecord::Base
has_many :endorsees
end
class Endorsees < ActiveRecord:: Base
belongs_to :draft
end
From my reading, methinks I should be able to say:
d = Draft.new
d.endorsees << Endorsee.new
SQL (for mysql) is:
create table drafts (
id int not null auto_increment,
claim_id int not null,
primary key(id)
);
create table endorsees (
id int not null auto_increment,
draft_id int not null,
constraint fk_endorsee_drafts
foreign key (draft_id) references drafts(id),
primary key(id)
) ;
I get the following error. The top stack point which references
my code is the line that does << assignment above, ie. d.endorsees <<
Endorsee.new
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]
RAILS_ROOT: ./script/…/config/…
Application Trace http://localhost:3000/claim_admin/create# |
Framework
Trace http://localhost:3000/claim_admin/create# | Full
Tracehttp://localhost:3000/claim_admin/create#
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1509:in
read_attribute' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1198:in
id’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1209:in
quoted_id' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/has_many_association.rb:169:in
construct_sql’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/has_many_association.rb:8:in
initialize' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:753:in
new’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:753:in
endorsees' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:749:in
endorsees’
#{RAILS_ROOT}/app/controllers/claim_admin_controller.rb:50:in `create’
#{RAILS_ROOT}/app/controllers/claim_admin_controller.rb:47:in each' #{RAILS_ROOT}/app/controllers/claim_admin_controller.rb:47:in
create’
#{RAILS_ROOT}/app/controllers/claim_admin_controller.rb:32:in `each’
#{RAILS_ROOT}/app/controllers/claim_admin_controller.rb:32:in `create’
What the heck am i missing?