Has_one and new

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

class PickupSearch < ActiveRecord::Base
belongs_to :searches

class DropoffSearch < ActiveRecord::Base
belongs_to :searches

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

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

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!


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs