0

I installed 'faker' gem but when I use it (Faker::Book.name) it doesn't display a random name, but only a string "Faker::Book"

lib > tasks > data_populate

require 'colorize'

namespace :data_populate do

namespace :populate do
    desc "Populate all model data set"
    task :all => :environment do
        require 'faker'
        puts "\n'Populating Model Data'".yellow
        Job.where(name: "Faker::Book").destroy_all
        10.times do |l|
            Job.create(name: Faker::Book.name, description: Faker::Lorem.sentence(7), business_id: 1)
        end
    end #all end
end #populate end
end #end rake dashboard

When the code runs, the Job Title is always "Faker::Book"

  • Did you try wrapping in curly brackets `Job.create(name: { Faker::Book.name } , description: { Faker::Lorem.sentence(7) }, business_id: 1)` – Joe Czucha May 27 '16 at 14:04
  • Just tried, gave me syntax error, unexpected '}', expecting => ...eate(name: { Faker::Book.name }, description: { Faker::Lorem... – Ankit Patel May 27 '16 at 14:12
  • Well first of all `"Faker::Book"` is a literal string so that is never going to be evaluated unless you call eval on it. Secondly you can't use `Job.where(name: Faker::Book)` - Faker::Book is a class - and calling the `name` or any of the other generator methods will generate a new unique value. – max May 27 '16 at 14:13
  • Max, I'm calling Faker::Book.name, but the return value is "Faker::Book", not a random value from the class call. – Ankit Patel May 27 '16 at 14:19
  • Just got it to work - there was a versioning issue -> I specified the correct latest version and reran bundle install. I think there might have been a change in a version regarding Book.name, Book.title, etc Also, calling Faker::Book directly to populate db isnt a good method, so I assigned to local variable and used that variable to populate Job.create Thanks all for quick morning responses!!! – Ankit Patel May 27 '16 at 14:22

1 Answers1

0

"Faker::Book" cant be stored to db, its name method just select a random name value, so it can't be also matched in where clause by class name, so you just try define some constance name part, and match over it:

Job.where("name LIKE 'faker%'").destroy_all
10.times do |l|
   Job.create(name: "faker " + Faker::Book.name, description: Faker::Lorem.sentence(7), business_id: 1)
end
max
  • 96,212
  • 14
  • 104
  • 165
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69