使用Faker Gem种子数据

时间:2016-06-09 00:00:31

标签: ruby-on-rails

我试图使用Faker gem来播种我的数据库,但我收到了一些错误消息,无法查看我出错的地方。我的seeds.rb是:

10.times do
  List.create! (
    name: Faker::Company.buzzword,
    shared_with: Faker::Internet.email,
    user_id: 3
  )
end

50.times do
  Item.create! (
    name: Faker::Company.bs,
    list_id: Faker::Number.between(1, 10),
    delegated_to: Faker::Internet.email,
    user_id: 3
  )
end

puts "Seed finished"
puts "#{List.count} lists created"
puts "#{Item.count} items created"

错误消息是:

rake aborted!
SyntaxError: /Users/.../db/seeds.rb:3: syntax error, unexpected tLABEL
    name: Faker::Company.buzzword,
         ^
/Users/.../db/seeds.rb:4: syntax error, unexpected tLABEL, expecting '='
    shared_with: Faker::Internet.email,
                ^
/Users/.../db/seeds.rb:5: syntax error, unexpected tLABEL, expecting '='
    user_id: 3
            ^
/Users/.../db/seeds.rb:11: syntax error, unexpected tLABEL
    name: Faker::Company.bs,
         ^
/Users/.../db/seeds.rb:12: syntax error, unexpected tLABEL, expecting '='
    list_id: Faker::Number.between(1, 10),
            ^
/Users/.../db/seeds.rb:12: syntax error, unexpected ',', expecting keyword_end
/Users/.../db/seeds.rb:14: syntax error, unexpected tLABEL, expecting '='
    user_id: 3
            ^
/Users/.../db/seeds.rb:20: syntax error, unexpected end-of-input, expecting keyword_end

有人能告诉我哪里出错了?

1 个答案:

答案 0 :(得分:2)

在Ruby中,你不应该在方法名和左括号之间加上空格。

# Syntax error
List.create! (

# Correct
List.create!(

因此,要扩展,您的代码应如下所示:

10.times do
  List.create!(
    name: Faker::Company.buzzword,
    shared_with: Faker::Internet.email,
    user_id: 3
  )
end