all. I'm currently adding to my seeds.rb
file in Rails using the faker
gem. I'm wondering: how do you get the fake data to follow "the rules" I want?
I'm building a basketball statistics application. I want stat
in seeds
to create 300 sets of statistics where all the criteria I have set in the stat
model are true. Right now, only 7-9 of the 300 sets of data end up being created. Is there a way to get seeds
to ignore the models that don't work and make 300 that do?
For instance, I want field goal attempts (fga
in my db) to be greater or equal to field goals made (fg
). (I have this "rule" set up in my model.) When I do this in my seeds file:
# seeds.rb snippet
300.times do
stat = Stat.create(
fg: Faker::Number.between(0, 15),
fga: Faker::Number.between(0, 20)
# more stats below
)
how do I make sure that fga
is >= fg
every time?
Do I have to say specifically in seeds
that fg
can't be greater than fga
? Or do I set a method in my stat.rb
model file and Faker
will follow it? (I have a few other rules on my model, otherwise I would just set the fake numbers differently.)
Thanks
until Stat.count >= 300 do
Stat.create(
fg: Faker::Number.between(0, 15),
fga: Faker::Number.between(0, 20)
# more stats below
)
end