# The error occurred while evaluating nil.prefetch\_primary\_key

**URL:** <https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil-prefetch-primary-key/178412>\
**Category:** Rails\
**Created:** [November 4, 2009, 7:14am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil-prefetch-primary-key/178412 "2009-11-04T07:14:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dk3qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dk3qu](https://www.ruby-forum.com/u/dk3qu)\
**Post date:** [November 4, 2009, 7:14am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil-prefetch-primary-key/178412/1 "2009-11-04T07:14:38Z")

</div>

The error occurred while evaluating nil.prefetch\_primary\_key?

Hi,

I have a problem with a model when I have 3 belongs\_to relations in one  
the middle.

This example is a subset of a grid-routing project:

```
                     n 1
       1 n +------------ Project

```

Connection ------- ConnectionCost  
±----------- WaySearchRun  
n 1  
The Problem is, when I add the ‘belongs\_to’ in ConnectionCost.  
I get the following error-message after creating a ConnectionCost :

> > ConnectionCost.create  
> > NoMethodError: You have a nil object when you didn’t expect it!  
> > The error occurred while evaluating nil.prefetch\_primary\_key?  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/base.rb:2721:in  
> > `create_without_callbacks' from /home/markus/Development/GPT2/my_test2/vendor/rails/activerecord/lib/active_record/callbacks.rb:237:in`create\_without\_timestamps’  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/timestamp.rb:29:in  
> > `create' from /home/markus/Development/GPT2/my_test2/vendor/rails/activerecord/lib/active_record/base.rb:2701:in`create\_or\_update\_without\_callbacks’  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/callbacks.rb:222:in  
> > `create_or_update' from /home/markus/Development/GPT2/my_test2/vendor/rails/activerecord/lib/active_record/base.rb:2385:in`save\_without\_validation’  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/validations.rb:1009:in  
> > `save_without_dirty' from /home/markus/Development/GPT2/my_test2/vendor/rails/activerecord/lib/active_record/dirty.rb:79:in`save\_without\_transactions’  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/transactions.rb:179:in  
> > `send' from /home/markus/Development/GPT2/my_test2/vendor/rails/activerecord/lib/active_record/transactions.rb:179:in`with\_transaction\_returning\_status’  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:66:in  
> > `transaction' from /home/markus/Development/GPT2/my_test2/vendor/rails/activerecord/lib/active_record/transactions.rb:129:in`transaction’  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/transactions.rb:178:in  
> > `with_transaction_returning_status' from /home/markus/Development/GPT2/my_test2/vendor/rails/activerecord/lib/active_record/transactions.rb:146:in`save’  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/transactions.rb:158:in  
> > `rollback_active_record_state!' from /home/markus/Development/GPT2/my_test2/vendor/rails/activerecord/lib/active_record/transactions.rb:146:in`save’  
> > from  
> > /home/markus/Development/GPT2/my\_test2/vendor/rails/activerecord/lib/active\_record/base.rb:693:in  
> > `create’  
> > from (irb):5\>\>

For this test I created a new rails-project (version 2.2.3).

Models :

class Connection \< ActiveRecord::Base  
has\_many :connection\_costs  
end

class Project \< ActiveRecord::Base  
has\_many :connection\_costs  
end

class WaySearchRun \< ActiveRecord::Base  
has\_many :connection\_costs  
end

class ConnectionCost \< ActiveRecord::Base  
belongs\_to :connection # dosn’t WORK !!!  
belongs\_to :project  
belongs\_to :way\_search\_run  
end

Migrations :

class CreateConnections \< ActiveRecord::Migration  
def self.up  
create\_table :connections do |t|  
t.string :name # Name  
t.string :alias # Alias-Name  
t.string :descr # Description  
t.timestamps  
end  
end

def self.down  
drop\_table :connections  
end  
end

class CreateProjects \< ActiveRecord::Migration  
def self.up  
create\_table :projects do |t|  
t.string :name # Name  
t.string :descr # Description  
t.timestamps  
end  
end

def self.down  
drop\_table :projects  
end  
end

class CreateWaySearchRuns \< ActiveRecord::Migration  
def self.up  
create\_table :way\_search\_runs do |t|  
t.string :name # Name  
t.string :descr # Description  
t.integer :sort\_number # for sorting - for a  
project in 10-steps  
t.references :project # Link to an ‘Project’  
t.timestamps  
end  
add\_index :way\_search\_runs, :project\_id  
end

def self.down  
drop\_table :way\_search\_runs  
end  
end

class CreateConnectionCosts \< ActiveRecord::Migration  
def self.up  
create\_table :connection\_costs do |t|  
t.decimal :cost ,:precision=\>10,:scale=\>4  
,:default=\>100.0 # absolute Costs  
t.decimal :cost\_factor ,:precision=\>10,:scale=\>4 ,:default=\>1.0

# CostFactor -\> totalCosts = CostFactor \* absCosts

```
  t.references :connection

```

# Link to an ‘Connection’

```
  t.references :project

```

# Link to an ‘Project’

```
  t.references :way_search_run

```

# Link to an ‘WaySearchRun’

```
  t.timestamps
end

add_index :connection_costs, :connection_id
add_index :connection_costs, :project_id
add_index :connection_costs, :way_search_run_id

```

end

def self.down  
drop\_table :connection\_costs  
end  
end

Thanks!
