Spasitel
Spasitel

Reputation: 169

Associations in seed.rb

I have following models in my app

class Building < ApplicationRecord

  has_many :rooms, dependent: :destroy
  ...

class Room < ApplicationRecord

  belongs_to :building
  has_many :lessons, dependent: :destroy
  ...

class Lesson < ApplicationRecord

  belongs_to :room
  belongs_to :teacher
  belongs_to :course
  ...

Everything worked fine between Bulding and its rooms with this code:

if Building.find_by_code("PAR").nil?
   building = Building.create!({title: "Areál Parukářka", code: "PAR"})
    
   par_rooms.each do |room|
      building.rooms << Room.create({title: room[0], code: room[1]})
   end 
end 

Now I want to add lessons to each Room. With the following code, no error is raised, and when I add some "puts" it says that the lessons has been created, but they are not available inside the controller/view. Here's the seed I use:

if Building.find_by_code("PAR").nil?
  building = Building.create!({title: "Areál Parukářka", code: "PAR"})

  par_rooms.each do |room|
    new_room = Room.create({title: room[0], code: room[1]})
    building.rooms << new_room
    lesson = Lesson.create({start_at: DateTime.new(2018, 11, 20, 8), end_at: DateTime.new(2018, 11, 20, 9, 30), duration: 45, room_id: new_room.id, teacher_id: nil, course_id: nil})
    new_room.lessons << lesson
  end

rooms and lessons tables has the following schema:

create_table "rooms", force: :cascade do |t|
    t.string "title"
    t.string "code"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "building_id"
    t.index ["building_id"], name: "index_rooms_on_building_id"
  end


create_table "lessons", force: :cascade do |t|
    t.datetime "start_at"
    t.datetime "end_at"
    t.integer "duration"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "room_id"
    t.integer "teacher_id"
    t.integer "course_id"
    t.index ["course_id"], name: "index_lessons_on_course_id"
    t.index ["room_id"], name: "index_lessons_on_room_id"
    t.index ["teacher_id"], name: "index_lessons_on_teacher_id"
  end

Upvotes: 4

Views: 321

Answers (2)

devanand
devanand

Reputation: 5290

lesson = Lesson.create({
    start_at: DateTime.new(2018, 11, 20, 8), 
    end_at: DateTime.new(2018, 11, 20, 9, 30), 
    duration: 45, room_id: new_room.id, 
    teacher_id: nil,     # is problematic with your model
    course_id: nil})     # is problematic with your model

Your model suggests that all relations are needed.

You should, if there are empty relations given, mark

belongs_to :teacher, optional: true

as optional.

Not that this solves your problem, but it should be the right direction.

For more ideas you should provide the schemas for teacher, course, room and building.

Upvotes: 1

lafeber
lafeber

Reputation: 2780

Try new_room = building.rooms.create({title: room[0], code: room[1]}) , and remove the line building.rooms << new_room

Upvotes: 0

Related Questions