Associations in seed.rb












4














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), durration: 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 "durration"
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









share|improve this question
























  • Did you run the seeds ? rails db:seed
    – Othmane El Kesri
    Nov 20 at 20:05










  • Yes I did that.
    – Spasitel
    Nov 20 at 20:06










  • Can you replace create by create! in your seeds and run them again to see if there is an error
    – Othmane El Kesri
    Nov 20 at 20:07










  • Theres an error raised "ActiveRecord::RecordInvalid: Validation failed: Building must exist" but when I run the server and open the app, I can see that bulding
    – Spasitel
    Nov 20 at 20:13










  • Also, theres "Building.destroy_all" at the beginning of the seed
    – Spasitel
    Nov 20 at 20:13
















4














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), durration: 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 "durration"
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









share|improve this question
























  • Did you run the seeds ? rails db:seed
    – Othmane El Kesri
    Nov 20 at 20:05










  • Yes I did that.
    – Spasitel
    Nov 20 at 20:06










  • Can you replace create by create! in your seeds and run them again to see if there is an error
    – Othmane El Kesri
    Nov 20 at 20:07










  • Theres an error raised "ActiveRecord::RecordInvalid: Validation failed: Building must exist" but when I run the server and open the app, I can see that bulding
    – Spasitel
    Nov 20 at 20:13










  • Also, theres "Building.destroy_all" at the beginning of the seed
    – Spasitel
    Nov 20 at 20:13














4












4








4







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), durration: 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 "durration"
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









share|improve this question















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), durration: 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 "durration"
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






ruby-on-rails ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 9:30

























asked Nov 20 at 20:03









Spasitel

777




777












  • Did you run the seeds ? rails db:seed
    – Othmane El Kesri
    Nov 20 at 20:05










  • Yes I did that.
    – Spasitel
    Nov 20 at 20:06










  • Can you replace create by create! in your seeds and run them again to see if there is an error
    – Othmane El Kesri
    Nov 20 at 20:07










  • Theres an error raised "ActiveRecord::RecordInvalid: Validation failed: Building must exist" but when I run the server and open the app, I can see that bulding
    – Spasitel
    Nov 20 at 20:13










  • Also, theres "Building.destroy_all" at the beginning of the seed
    – Spasitel
    Nov 20 at 20:13


















  • Did you run the seeds ? rails db:seed
    – Othmane El Kesri
    Nov 20 at 20:05










  • Yes I did that.
    – Spasitel
    Nov 20 at 20:06










  • Can you replace create by create! in your seeds and run them again to see if there is an error
    – Othmane El Kesri
    Nov 20 at 20:07










  • Theres an error raised "ActiveRecord::RecordInvalid: Validation failed: Building must exist" but when I run the server and open the app, I can see that bulding
    – Spasitel
    Nov 20 at 20:13










  • Also, theres "Building.destroy_all" at the beginning of the seed
    – Spasitel
    Nov 20 at 20:13
















Did you run the seeds ? rails db:seed
– Othmane El Kesri
Nov 20 at 20:05




Did you run the seeds ? rails db:seed
– Othmane El Kesri
Nov 20 at 20:05












Yes I did that.
– Spasitel
Nov 20 at 20:06




Yes I did that.
– Spasitel
Nov 20 at 20:06












Can you replace create by create! in your seeds and run them again to see if there is an error
– Othmane El Kesri
Nov 20 at 20:07




Can you replace create by create! in your seeds and run them again to see if there is an error
– Othmane El Kesri
Nov 20 at 20:07












Theres an error raised "ActiveRecord::RecordInvalid: Validation failed: Building must exist" but when I run the server and open the app, I can see that bulding
– Spasitel
Nov 20 at 20:13




Theres an error raised "ActiveRecord::RecordInvalid: Validation failed: Building must exist" but when I run the server and open the app, I can see that bulding
– Spasitel
Nov 20 at 20:13












Also, theres "Building.destroy_all" at the beginning of the seed
– Spasitel
Nov 20 at 20:13




Also, theres "Building.destroy_all" at the beginning of the seed
– Spasitel
Nov 20 at 20:13












2 Answers
2






active

oldest

votes


















1














lesson = Lesson.create({
start_at: DateTime.new(2018, 11, 20, 8),
end_at: DateTime.new(2018, 11, 20, 9, 30),
durration: 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.






share|improve this answer





















  • Thanks, that makes perfect sense. I've marked id's that doesn't exist in the moment when a lesson is created and it works as expected.
    – Spasitel
    Nov 21 at 9:42



















0














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






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400717%2fassociations-in-seed-rb%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    lesson = Lesson.create({
    start_at: DateTime.new(2018, 11, 20, 8),
    end_at: DateTime.new(2018, 11, 20, 9, 30),
    durration: 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.






    share|improve this answer





















    • Thanks, that makes perfect sense. I've marked id's that doesn't exist in the moment when a lesson is created and it works as expected.
      – Spasitel
      Nov 21 at 9:42
















    1














    lesson = Lesson.create({
    start_at: DateTime.new(2018, 11, 20, 8),
    end_at: DateTime.new(2018, 11, 20, 9, 30),
    durration: 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.






    share|improve this answer





















    • Thanks, that makes perfect sense. I've marked id's that doesn't exist in the moment when a lesson is created and it works as expected.
      – Spasitel
      Nov 21 at 9:42














    1












    1








    1






    lesson = Lesson.create({
    start_at: DateTime.new(2018, 11, 20, 8),
    end_at: DateTime.new(2018, 11, 20, 9, 30),
    durration: 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.






    share|improve this answer












    lesson = Lesson.create({
    start_at: DateTime.new(2018, 11, 20, 8),
    end_at: DateTime.new(2018, 11, 20, 9, 30),
    durration: 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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 at 9:30









    devanand

    3,3271318




    3,3271318












    • Thanks, that makes perfect sense. I've marked id's that doesn't exist in the moment when a lesson is created and it works as expected.
      – Spasitel
      Nov 21 at 9:42


















    • Thanks, that makes perfect sense. I've marked id's that doesn't exist in the moment when a lesson is created and it works as expected.
      – Spasitel
      Nov 21 at 9:42
















    Thanks, that makes perfect sense. I've marked id's that doesn't exist in the moment when a lesson is created and it works as expected.
    – Spasitel
    Nov 21 at 9:42




    Thanks, that makes perfect sense. I've marked id's that doesn't exist in the moment when a lesson is created and it works as expected.
    – Spasitel
    Nov 21 at 9:42













    0














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






    share|improve this answer


























      0














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






      share|improve this answer
























        0












        0








        0






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






        share|improve this answer












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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 9:17









        lafeber

        9811221




        9811221






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400717%2fassociations-in-seed-rb%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen