# Has\_one and new

**URL:** <https://www.ruby-forum.com/t/has-one-and-new/174402>\
**Category:** Rails\
**Created:** [August 26, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/has-one-and-new/174402 "2009-08-26T13:54:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 26, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/has-one-and-new/174402/1 "2009-08-26T13:54:40Z")

</div>

Hi Folks,

I’m totally confused - I have 3 tables

search, pickup\_search and dropoff\_search

pickup\_search and dropoff\_search are nearly identical and each contain  
the search\_id key.

My Models are as follows.

class Search \< ActiveRecord::Base  
has\_one :pickup\_search  
has\_one :dropoff\_search  
end

class PickupSearch \< ActiveRecord::Base  
belongs\_to :searches  
end

class DropoffSearch \< ActiveRecord::Base  
belongs\_to :searches  
end

So what I want to do is create a new search, then a new pickup\_search  
and a new dropoff\_search.

I tried in my controllers new method to do

def new  
@search = Search.new  
@search.pickup\_search.new  
@search.dropoff\_search.new  
end

however it throws the error when creating a new pickup and dropoff  
search

NoMethodError (You have a nil object when you didn’t expect it!

However if I change the relationship in search to has\_many it works!!!

However for any given ‘search’ there will only be one pickup\_search and  
dropoff\_Search so it shouldn’t be a has\_many relationship… or am I  
modelling this incorrectly

(FYI the pickup\_search and dropoff\_date contains date/destination info)

Any help much appreciated!

Peter
