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!