# STI questions

**URL:** <https://www.ruby-forum.com/t/sti-questions/109890>\
**Category:** Rails\
**Created:** [July 19, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/sti-questions/109890 "2007-07-19T23:09:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 19, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/sti-questions/109890/1 "2007-07-19T23:09:00Z")

</div>

i have an Order model and a table for addresses. i was trying to use  
single table inheritance so that i can store the addresses in the same  
table.

here is my order model:

class Order \< ActiveRecord::Base

has\_one :billing\_address, :class\_name =\>  
‘BillingAddress’, :foreign\_key =\> ‘order\_id’  
has\_one :shipping\_address, :class\_name =\>  
‘ShippingAddress’, :foreign\_key =\> ‘order\_id’  
end

and the address model:

class Address \< ActiveRecord::Base  
end

class BillingAddress \< Address  
belongs\_to :order, :class\_name =\> ‘Order’  
end

class ShippingAddress \< Address  
belongs\_to :order, :class\_name =\> ‘Order’  
end

everything appears to be working fine, unless i try and do something  
like this:

my\_order.billing\_address.create(…)

i get this in the console:

NoMethodError: You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.create

however, if i create an address this way:

BillingAddress.create(… :order =\> my\_order) it works fine. i can  
even use order.billing\_address and it will pull up the right record.

is this right, or did i set up the relationships wrong?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 20, 2007, 1:12am UTC](https://www.ruby-forum.com/t/sti-questions/109890/2 "2007-07-20T01:12:16Z")

</div>

Josh wrote:

> NoMethodError: You have a nil object when you didn’t expect it!  
> You might have expected an instance of ActiveRecord::Base.  
> The error occurred while evaluating nil.create

The correct syntax is my\_order.create\_billing\_address(…)

–  
We develop, watch us RoR, in numbers too big to ignore.
