query about create has_many & belongs_to records using FactoryBot












0















I am writing Rspec requests spec, and before it I want to build some test data using FactoryBot.



And now I have a model Game:



class Game < ApplicationRecord
has_many :game_levels


and a model GameLevel:



class GameLevel < ApplicationRecord
belongs_to :game


In my /spec/factories/game.rb:



FactoryBot.define do
factory :game do
name { :Mario }
end
end


In my spec/factories/game_level.rb:



FactoryBot.define do
factory :game_level do
name { :default }
min_level { 0 }
max_level { 100 }

game
end
end


In my spec/requests/user_plays_game_spec.rb, I simply wrote code to create game & game_level, and printed game.id, game_level.game_id. I found they are not the same. besides, game.game_levels returns nil.



before(:all) do
@game = create(:game)
@game_level = create(:game_level)
end

describe do
it do
puts @game,id, @game_level.game_id
puts @game.game_levels

expect(@game.id).to eql(@game_level.game_id)
end
end


So how do I associate a belongs_to record to a has_many record using FactoryBot?










share|improve this question























  • do you need to do this in your tests or in your factory definitions?

    – potashin
    Nov 22 '18 at 8:13
















0















I am writing Rspec requests spec, and before it I want to build some test data using FactoryBot.



And now I have a model Game:



class Game < ApplicationRecord
has_many :game_levels


and a model GameLevel:



class GameLevel < ApplicationRecord
belongs_to :game


In my /spec/factories/game.rb:



FactoryBot.define do
factory :game do
name { :Mario }
end
end


In my spec/factories/game_level.rb:



FactoryBot.define do
factory :game_level do
name { :default }
min_level { 0 }
max_level { 100 }

game
end
end


In my spec/requests/user_plays_game_spec.rb, I simply wrote code to create game & game_level, and printed game.id, game_level.game_id. I found they are not the same. besides, game.game_levels returns nil.



before(:all) do
@game = create(:game)
@game_level = create(:game_level)
end

describe do
it do
puts @game,id, @game_level.game_id
puts @game.game_levels

expect(@game.id).to eql(@game_level.game_id)
end
end


So how do I associate a belongs_to record to a has_many record using FactoryBot?










share|improve this question























  • do you need to do this in your tests or in your factory definitions?

    – potashin
    Nov 22 '18 at 8:13














0












0








0








I am writing Rspec requests spec, and before it I want to build some test data using FactoryBot.



And now I have a model Game:



class Game < ApplicationRecord
has_many :game_levels


and a model GameLevel:



class GameLevel < ApplicationRecord
belongs_to :game


In my /spec/factories/game.rb:



FactoryBot.define do
factory :game do
name { :Mario }
end
end


In my spec/factories/game_level.rb:



FactoryBot.define do
factory :game_level do
name { :default }
min_level { 0 }
max_level { 100 }

game
end
end


In my spec/requests/user_plays_game_spec.rb, I simply wrote code to create game & game_level, and printed game.id, game_level.game_id. I found they are not the same. besides, game.game_levels returns nil.



before(:all) do
@game = create(:game)
@game_level = create(:game_level)
end

describe do
it do
puts @game,id, @game_level.game_id
puts @game.game_levels

expect(@game.id).to eql(@game_level.game_id)
end
end


So how do I associate a belongs_to record to a has_many record using FactoryBot?










share|improve this question














I am writing Rspec requests spec, and before it I want to build some test data using FactoryBot.



And now I have a model Game:



class Game < ApplicationRecord
has_many :game_levels


and a model GameLevel:



class GameLevel < ApplicationRecord
belongs_to :game


In my /spec/factories/game.rb:



FactoryBot.define do
factory :game do
name { :Mario }
end
end


In my spec/factories/game_level.rb:



FactoryBot.define do
factory :game_level do
name { :default }
min_level { 0 }
max_level { 100 }

game
end
end


In my spec/requests/user_plays_game_spec.rb, I simply wrote code to create game & game_level, and printed game.id, game_level.game_id. I found they are not the same. besides, game.game_levels returns nil.



before(:all) do
@game = create(:game)
@game_level = create(:game_level)
end

describe do
it do
puts @game,id, @game_level.game_id
puts @game.game_levels

expect(@game.id).to eql(@game_level.game_id)
end
end


So how do I associate a belongs_to record to a has_many record using FactoryBot?







ruby-on-rails factory-bot has-many belongs-to






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 8:07









weird honeyweird honey

17310




17310













  • do you need to do this in your tests or in your factory definitions?

    – potashin
    Nov 22 '18 at 8:13



















  • do you need to do this in your tests or in your factory definitions?

    – potashin
    Nov 22 '18 at 8:13

















do you need to do this in your tests or in your factory definitions?

– potashin
Nov 22 '18 at 8:13





do you need to do this in your tests or in your factory definitions?

– potashin
Nov 22 '18 at 8:13












2 Answers
2






active

oldest

votes


















5














You can associate it during the create



@game = create(:game)
@game_level = create(:game_level, game: @game)





share|improve this answer
























  • thanks for help, it worked perfectly as I expected!

    – weird honey
    Nov 22 '18 at 8:33



















0














Associations in factories are intuitive:



FactoryBot.define do
factory :template do
template_category { create(:template_category) }
end
end


Nothing fancy. If it's not working for you you might have a config issue.






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%2f53426364%2fquery-about-create-has-many-belongs-to-records-using-factorybot%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









    5














    You can associate it during the create



    @game = create(:game)
    @game_level = create(:game_level, game: @game)





    share|improve this answer
























    • thanks for help, it worked perfectly as I expected!

      – weird honey
      Nov 22 '18 at 8:33
















    5














    You can associate it during the create



    @game = create(:game)
    @game_level = create(:game_level, game: @game)





    share|improve this answer
























    • thanks for help, it worked perfectly as I expected!

      – weird honey
      Nov 22 '18 at 8:33














    5












    5








    5







    You can associate it during the create



    @game = create(:game)
    @game_level = create(:game_level, game: @game)





    share|improve this answer













    You can associate it during the create



    @game = create(:game)
    @game_level = create(:game_level, game: @game)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 8:14









    SteveTurczynSteveTurczyn

    26.7k42738




    26.7k42738













    • thanks for help, it worked perfectly as I expected!

      – weird honey
      Nov 22 '18 at 8:33



















    • thanks for help, it worked perfectly as I expected!

      – weird honey
      Nov 22 '18 at 8:33

















    thanks for help, it worked perfectly as I expected!

    – weird honey
    Nov 22 '18 at 8:33





    thanks for help, it worked perfectly as I expected!

    – weird honey
    Nov 22 '18 at 8:33













    0














    Associations in factories are intuitive:



    FactoryBot.define do
    factory :template do
    template_category { create(:template_category) }
    end
    end


    Nothing fancy. If it's not working for you you might have a config issue.






    share|improve this answer




























      0














      Associations in factories are intuitive:



      FactoryBot.define do
      factory :template do
      template_category { create(:template_category) }
      end
      end


      Nothing fancy. If it's not working for you you might have a config issue.






      share|improve this answer


























        0












        0








        0







        Associations in factories are intuitive:



        FactoryBot.define do
        factory :template do
        template_category { create(:template_category) }
        end
        end


        Nothing fancy. If it's not working for you you might have a config issue.






        share|improve this answer













        Associations in factories are intuitive:



        FactoryBot.define do
        factory :template do
        template_category { create(:template_category) }
        end
        end


        Nothing fancy. If it's not working for you you might have a config issue.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 8:19









        well-i-better-get-rollingwell-i-better-get-rolling

        3,45432860




        3,45432860






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426364%2fquery-about-create-has-many-belongs-to-records-using-factorybot%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin