|
1 |
| - |
2 | 1 | # db/seeds.rb
|
3 | 2 |
|
4 |
| -# Create Puzzle records |
5 |
| -Puzzle.create!([ |
6 |
| - { question: "Ruby or Rails provided this method? Array.new(5) { |i| i * 2 }", answer: :ruby, explanation: "`Array.new` is a core Ruby method that creates a new array with a specified size and optional block for initialization. This is part of Ruby’s core library." }, |
7 |
| - { question: "Ruby or Rails provided this method? File.open('log.txt', 'w') { |file| file.write('Hello, World!') }", answer: :ruby, explanation: "`File.open` is a core Ruby method for opening files. It’s part of Ruby's standard library for file handling, not part of Rails." }, |
8 |
| - { question: "Ruby or Rails provided this method? render json: @user", answer: :rails, explanation: "`render json:` is a Rails method used to render a JSON response from a controller action." }, |
9 |
| - { question: "Ruby or Rails provided this method? link_to 'Home', root_path", answer: :rails, explanation: "`link_to` is a Rails helper method that generates HTML links. `root_path` is a Rails path helper." }, |
10 |
| - { question: "Ruby or Rails provided this method? params[:id]", answer: :rails, explanation: "`params[:id]` is used in Rails to fetch query parameters or URL parameters in controller actions." } |
11 |
| -]) |
| 3 | +# ====== Create Puzzle records ====== |
| 4 | +puzzles = [ |
| 5 | + { |
| 6 | + question: "Ruby or Rails provided this method? Array.new(5) { |i| i * 2 }", |
| 7 | + answer: :ruby, |
| 8 | + explanation: "`Array.new` is a core Ruby method that creates a new array with a specified size and optional block for initialization. This is part of Ruby’s core library." |
| 9 | + }, |
| 10 | + { |
| 11 | + question: "Ruby or Rails provided this method? File.open('log.txt', 'w') { |file| file.write('Hello, World!') }", |
| 12 | + answer: :ruby, |
| 13 | + explanation: "`File.open` is a core Ruby method for opening files. It’s part of Ruby's standard library for file handling, not part of Rails." |
| 14 | + }, |
| 15 | + { |
| 16 | + question: "Ruby or Rails provided this method? render json: @user", |
| 17 | + answer: :rails, |
| 18 | + explanation: "`render json:` is a Rails method used to render a JSON response from a controller action." |
| 19 | + }, |
| 20 | + { |
| 21 | + question: "Ruby or Rails provided this method? link_to 'Home', root_path", |
| 22 | + answer: :rails, |
| 23 | + explanation: "`link_to` is a Rails helper method that generates HTML links. `root_path` is a Rails path helper." |
| 24 | + }, |
| 25 | + { |
| 26 | + question: "Ruby or Rails provided this method? params[:id]", |
| 27 | + answer: :rails, |
| 28 | + explanation: "`params[:id]` is used in Rails to fetch query parameters or URL parameters in controller actions." |
| 29 | + } |
| 30 | +] |
| 31 | + |
| 32 | +puzzles.each do |p| |
| 33 | + Puzzle.find_or_create_by!(question: p[:question]) do |puzzle| |
| 34 | + puzzle.answer = p[:answer] |
| 35 | + puzzle.explanation = p[:explanation] |
| 36 | + end |
| 37 | +end |
12 | 38 |
|
13 |
| -# Create a server |
14 |
| -Server.create!(server_id: 1179555097060061245, name: "OmbuTest") |
| 39 | +# ====== Create the Server ====== |
| 40 | +server = Server.find_or_create_by!(server_id: 1179555097060061245) do |s| |
| 41 | + s.name = "OmbuTest" |
| 42 | +end |
15 | 43 |
|
16 |
| -# Seed data for 10 users with answers |
| 44 | +# ====== Create Users and associate with Server ====== |
17 | 45 | users = [
|
18 | 46 | { user_id: 101, username: "user1", role: "member" },
|
19 | 47 | { user_id: 102, username: "user2", role: "member" },
|
|
28 | 56 | ]
|
29 | 57 |
|
30 | 58 | users.each do |user_data|
|
31 |
| - user = User.create(user_id: user_data[:user_id], username: user_data[:username], role: user_data[:role]) |
| 59 | + user = User.find_or_create_by!(user_id: user_data[:user_id]) do |u| |
| 60 | + u.username = user_data[:username] |
| 61 | + u.role = user_data[:role] |
| 62 | + end |
| 63 | + |
| 64 | + # Associate user with the server if not already linked |
| 65 | + user.servers << server unless user.servers.include?(server) |
32 | 66 |
|
33 |
| - # Randomly assign scores to answers (you can adjust as needed) |
34 |
| - 3.times do |
35 |
| - Answer.create!( |
36 |
| - user_id: user.id, |
37 |
| - puzzle_id: Puzzle.all.sample.id, |
38 |
| - server_id: Server.first.id, |
39 |
| - choice: [ "ruby", "rails" ].sample, |
40 |
| - is_correct: [ true, false ].sample |
41 |
| - ) |
| 67 | + # Seed random answers for this user if they have none |
| 68 | + if user.answers.where(server_id: server.id).empty? |
| 69 | + 3.times do |
| 70 | + Answer.create!( |
| 71 | + user_id: user.id, |
| 72 | + puzzle_id: Puzzle.all.sample.id, |
| 73 | + server_id: server.id, |
| 74 | + choice: [ "ruby", "rails" ].sample, |
| 75 | + is_correct: [ true, false ].sample |
| 76 | + ) |
| 77 | + end |
42 | 78 | end
|
43 | 79 | end
|
0 commit comments