Has_many reference

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?

Hi, Rob,

here’s a guess: You haven’t got an endorsees Array yet. .new() is just
initializing Objects that inherit from the ActiveRecord-Base. For the
relationship part you need some db generated ids (autoincrement). They
you
don’t have because you’ve issued for none of the Objects an .save(). In
your
examples you don’t even have any attributes written to your objects. You
might try:

draft = Draft.new(params[:draft]) # params[:draft] is from your form in
the
view
endorsee = Endorse.new(params[:endorsee])
if draft.save
draft.endorsees << endorsee
end

or even

draft = Draft.new(params[:draft]) # params[:draft] is from your form in
the
view
if draft.save
draft.endorsees.create(params[:endorsee])
end

tinker with these and always (at least for the beginning) have a tight
look
on the sql in your development.log to see whats happening under the
covers.

Best Regards
Jan P.

http://www.openization.com - share your ideas…
http://blog.openization.com - read about mine…

On 2/23/06, Jan P. [email protected] wrote:

draft = Draft.new(params[:draft]) # params[:draft] is from your form in
if draft.save
draft.endorsees.create(params[:endorsee])
end

I tried to save draft and it still didn’t work. I got a very similar
error.
I look into base.rb code and it is crapping out when it finds the
@attributes instance variable of the Base class to be nil.

tinker with these and always (at least for the beginning) have a tight
look


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails