# Has\_one relationship not working

**URL:** <https://www.ruby-forum.com/t/has-one-relationship-not-working/111858>\
**Category:** Rails\
**Created:** [August 12, 2007, 1:02pm UTC](https://www.ruby-forum.com/t/has-one-relationship-not-working/111858 "2007-08-12T13:02:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [August 12, 2007, 1:02pm UTC](https://www.ruby-forum.com/t/has-one-relationship-not-working/111858/1 "2007-08-12T13:02:34Z")

</div>

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  
…

mysql\> show fields from creditcards;  
±-----------------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±-----------------±-------------±-----±----±--------±---------------+  
…  
| vmorder\_id | int(11) | YES | | NULL | |  
±-----------------±-------------±-----±----±--------±---------------+  
16 rows in set (0.00 sec)  
mysql\>

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?

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [August 12, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/has-one-relationship-not-working/111858/2 "2007-08-12T14:52:40Z")

</div>

James Sturrock wrote:

> ±-----------------±-------------±-----±----±--------±---------------+  
> NoMethodError: You have a nil object when you didn’t expect it!  
> /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.

mysql\> desc creditcards;  
±-----------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±-----------±-------------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
| name | varchar(255) | YES | | NULL | |  
| vmorder\_id | int(11) | YES | | NULL | |  
±-----------±-------------±-----±----±--------±---------------+

mysql\> desc vmorders;  
±------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±------±-------------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
| name | varchar(255) | YES | | NULL | |  
±------±-------------±-----±----±--------±---------------+

class Vmorder \< ActiveRecord::Base  
has\_one :creditcard  
end

class Creditcard \< ActiveRecord::Base  
belongs\_to :vmorder  
end

> > vmorder = Vmorder.new  
> > =\> #\<Vmorder:0x349a608 @new\_record=true, @attributes={“name”=\>nil}\>
> 
> > creditcard = Creditcard.new  
> > =\> #\<Creditcard:0x348a17c @new\_record=true, @attributes={“name”=\>nil,  
> > “vmorder\_id”=\>nil}\>
> 
> > vmorder.creditcard = creditcard  
> > =\> #\<Creditcard:0x348a17c @new\_record=true, @attributes={“name”=\>nil,  
> > “vmorder\_id”=\>nil}\>
> 
> > vmorder.creditcard  
> > =\> #\<Creditcard:0x348a17c @new\_record=true, @attributes={“name”=\>nil,  
> > “vmorder\_id”=\>nil}\>

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [August 12, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/has-one-relationship-not-working/111858/3 "2007-08-12T19:30:09Z")

</div>

I have figured out the problem.

in the vmorder class I had an initialise method but I didn’t call  
super() from within the method.
