has_one relationship not working

James Sturrock wrote:

I'm writing my first web app in rails and it was going really well until now.

I have just linked 2 class with a has_one relationship but its not working.

class Vmorder < ActiveRecord::Base   has_one :creditcard .....

class Creditcard < ActiveRecord::Base   belongs_to :vmorder .....

> show fields from creditcards; +------------------+--------------+------+-----+---------+----------------+ > Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+----------------+ ............ > vmorder_id | int(11) | YES | | NULL | | +------------------+--------------+------+-----+---------+----------------+ 16 rows in set (0.00 sec) >

this is what I get from the ruby console:

?> vmorder = Vmorder.new ?> creditcard = Creditcard.new ?> ?> vmorder.creditcard = creditcard NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.         from (eval):1:in `id'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1529:in `quoted_id'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_one_association.rb:74:in `construct_sql'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_one_association.rb:6:in `initialize'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations.rb:905:in `creditcard='         from (irb):64

I really can't see what i'm doing wrong here, has anybody got any ideas?

Worked for me. Trying stripping down your models and migrations to their bare minimums in a test app and see what you get.