jeudi 31 mars 2016

Multiple t.references get nil value for the second nested models

I have visits who belongs_to owner, and belongs_to car through two t.references, owner_id keeps getting nil when I create new visit through @car.visits.create.

Visit.rb

class Visit < ActiveRecord::Base
#validates :notes, presence:true
belongs_to :car
belongs_to :owner
end

Owner.rb

class Owner < ActiveRecord::Base
has_many :cars
has_many :visits    
accepts_nested_attributes_for :visits
accepts_nested_attributes_for :cars
end

Car.rb

class Car < ActiveRecord::Base
belongs_to :owner
has_many :visits
end

The migration for Visit

class CreateVisits < ActiveRecord::Migration
def change
create_table :visits do |t|
  t.boolean :open
  t.text :notes
  t.references :owner
  t.references :car
  t.timestamps null: false
end

end
end

if I

 car1 = Car.create(......)
 car1.visits.create(.....)

I get the value of Visit.owner_id = nil, however, car_id is perfectly filled with the right relation.

what am I missing? Help is much appreciated.

example from my console where owner_id: nil even when its created from a @car who has already owner_id

Aucun commentaire:

Enregistrer un commentaire