query about create has_many & belongs_to records using FactoryBot
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
add a comment |
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
do you need to do this in your tests or in your factory definitions?
– potashin
Nov 22 '18 at 8:13
add a comment |
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
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
ruby-on-rails factory-bot has-many belongs-to
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
You can associate it during the create
@game = create(:game)
@game_level = create(:game_level, game: @game)
thanks for help, it worked perfectly as I expected!
– weird honey
Nov 22 '18 at 8:33
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You can associate it during the create
@game = create(:game)
@game_level = create(:game_level, game: @game)
thanks for help, it worked perfectly as I expected!
– weird honey
Nov 22 '18 at 8:33
add a comment |
You can associate it during the create
@game = create(:game)
@game_level = create(:game_level, game: @game)
thanks for help, it worked perfectly as I expected!
– weird honey
Nov 22 '18 at 8:33
add a comment |
You can associate it during the create
@game = create(:game)
@game_level = create(:game_level, game: @game)
You can associate it during the create
@game = create(:game)
@game_level = create(:game_level, game: @game)
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 22 '18 at 8:19
well-i-better-get-rollingwell-i-better-get-rolling
3,45432860
3,45432860
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
do you need to do this in your tests or in your factory definitions?
– potashin
Nov 22 '18 at 8:13