Forum: Ruby on Rails has_many reference

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bc6a0d662e93934006ede63a14331d1c?d=identicon&s=25 Rob Gordon (Guest)
on 2006-02-23 08:31
(Received via mailing list)
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
Trace<http://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?
E48d29dc8fedb2878fa518d41cc63d88?d=identicon&s=25 Jan Prill (Guest)
on 2006-02-23 09:17
(Received via mailing list)
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 Prill

http://www.openization.com - share your ideas...
http://blog.openization.com - read about mine...
1aa112c30efa4109f938e7676c3808b6?d=identicon&s=25 Mats Lindblad (Guest)
on 2006-02-23 09:26
(Received via mailing list)
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Bc6a0d662e93934006ede63a14331d1c?d=identicon&s=25 Rob Gordon (Guest)
on 2006-02-23 20:08
(Received via mailing list)
On 2/23/06, Jan Prill <JanPrill@blauton.de> 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
This topic is locked and can not be replied to.