I have 2 models: TripPlan and Place. The models are like below:
# app/models/trip_plan.rb
class TripPlan < ActiveRecord::Base
has_many :places
end
# app/models/place.rb
class Place < ActiveRecord::Base
belongs_to :trip_plan
end
There's a corresponding migration for the places table as well:
class CreatePlaces < ActiveRecord::Migration
def change
create_table :places do |t|
t.references :trip_plan, index: true
t.timestamps null: false
end
end
end
So each TripPlan can have several places and each Place belongs to a single trip plan. But now I need a has_one/belongs_to relation between these models. I have modified TripPlan model as follows:
class TripPlan < ActiveRecord::Base
has_one :place
end
But now if I try TripPlan.find(1).place.build it gives an error that undefined method 'build' for nil:NilClass. How can I fix this?
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire