Question

I have a rake task that populates my db with seed data using FactoryGirl. For example -

def create_goals
  puts "Creating goals for each subject"
  Subject.all.each do |s|
    FactoryGirl.create_list :goal, 3, subject_id: s.id
  end
end

I have a new model - Evaluations - that belongs_to two other models: Students and Goals.

At the moment I have this:

def create_evaluations
  puts "Creating evaluation data for each goal (have you put the kettle on yet?)"
  Goal.all.each do |g|
    FactoryGirl.create_list :evaluation, 3, goal_id: g.id
  end    
end

but this obviously only gives data that has a goal_id - I've made the same thing with a Student parent, but then the problem is reversed - so how to make data for a model with two belongs_to associations?

UPDATE:

I tried this:

def create_evaluations
  puts "Creating evaluation data for each student and goal"
  Student.all.each do |s|
    Goal.all.each do |g|
      FactoryGirl.create_list :evaluation, 3, student_id: s.id, goal_id: g.id
    end
  end     
end

but haven't been able to test if it works as I let it run for ~1 hour and was still running.

UPDATE: The reason it was running so long is because I neglected to change the amount of users it was generating. I changed it to only 2 users and it ran quickly enough. Here's the code that I ended up using (thanks to Pierre-Louis Gottfrois for your help!):

require 'factory_girl_rails'

namespace :db do

  desc "Fill db with sample data"

  task :populate, :environment do

    # warning message
    puts "##############################################################"
    puts "# if this takes too long, reduce the number of created users #"
    puts "##############################################################"

    # reset the database
    reset_db

    # some messages
    puts "Database reset. Database population started"

    # create users
    create_users

    #create student_groups for each user  
    create_student_groups

    #create students for each student_group
    create_students

    #create subjects for each student_group
    create_subjects

    #create goals for each subject
    create_goals

    #create evaluation data for each goal
    create_evaluations

    # success message
    puts "The database has been populated successfully"

  end

###################
####  METHODS  ####
###################

  def reset_db
    puts "Resetting the database"
    Rake::Task['db:reset'].invoke
  end

  def create_users
    puts "Creating users"
    FactoryGirl.create_list :user, 2
  end

  def create_student_groups
    puts "Creating student groups for each user"
    User.all.each do |u|
      FactoryGirl.create_list :student_group, 3, user_id: u.id
    end
  end

  def create_students
    puts "Creating students for each student group"
    StudentGroup.all.each do |sg|
      FactoryGirl.create_list :student, 7, student_group_id: sg.id
    end
  end

  def create_subjects
    puts "Creating subjects for each student group"
    StudentGroup.all.each do |sg|
      FactoryGirl.create_list :subject, 4, student_group_id: sg.id
    end
  end  

  def create_goals
    puts "Creating goals for each subject"
    Subject.all.each do |s|
      FactoryGirl.create_list :goal, 3, subject_id: s.id
    end
  end  

  def create_evaluations
    puts "Creating evaluation data"
    Student.all.each_with_index do |s, index|
      @goals = Goal.all  
      @goals.count.times do |i|
        FactoryGirl.create_list :evaluation, 3, goal_id: @goals[i].id, student_id: s.id
      end
    # Counter to track progress
    puts "Created data for 'Student #{index}'"  
    end
  end

end
Was it helpful?

Solution

Not sure to understand your issue here. Is this helping ?

@students = Student.all

@students.count.times do |i|
  FactoryGirl.create_list :evaluation, 3, goal_id: g.id, student_id: @students[i].id
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top