In rails, I wrote a task for automatically generate fake user
task fake_user: :environment do
User.destroy_all
filelink = ""
Dir.glob("#{Rails.root}/public/avatar/admin.jpg").map do |pic|
client = FilestackClient.new('ARz3g0HLfR5i0KuobcdJmz')
filelink = client.upload(filepath: pic)
end
admin = User.create(
email: "admin#example.com",
password: "admin123",
role: 'admin',
name: 'admin',
introduction: FFaker::Lorem::sentence(30),
avatar: filelink.url
)
puts admin.name
20.times do |i|
name = FFaker::Name::first_name
filelink = ""
Dir.glob("#{Rails.root}/public/avatar/user#{i+1}.jpg").map do |pic|
client = FilestackClient.new('ARz3g0HLfR5i0KuobcdJmz')
filelink = client.upload(filepath: pic)
end
user = User.create(
name: name,
email: "#{name}#example.co",
password: "12345678",
introduction: FFaker::Lorem::sentence(30),
avatar: filelink.url
)
user.save!
puts user.name
end
end
And there's no error when running in local.
But when I deploy it to Heroku, some errors happened
Here's the steps I did
Heroku run bundle install (correct)
Heroku run rails db:migrate (correct)
Heroku run rails dev:fake_user (error...)
Here's the error message I got
Hope someone can help me fix this.... thanks....
From logs it seems that your connection is disconnected. Have you tried following in database.yml ?
reconnect: true
Related
I am running through Hartl's Rails tutorial, but when I try to seed Microposts I am getting the error: "ActiveRecord::RecordInvalid: Validation failed: Email has already been taken"
I did db:migrate:reset followed by db:seed which throws up the error. Is there something wrong with what I am trying to seed?
User.create!(name: "Example User",
email: "example#railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
99.times do |n|
name= Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password,
activated: true,
activated_at: Time.zone.now)
end
users = User.order(:created_at).take(6)
50.times do
content = Faker::Lorem.sentence(word_count: 5)
users.each { |user| user.microposts.create!(content: content) }
end
You have probably already created some records in the database, which invokes validation. To check this, run the console rails c and then type User.first.
If you have some records run rake db:reset to reset database.
If you want to be able to run the seed multiple times, write
User.find_or_create_by(email: email) do |u|
u.name: name,
u.password: password,
u.password_confirmation: password,
u.activated: true,
u.activated_at: Time.zone.now
end
instead your code.
Instead of create! user create so if the email address is already taken, the record would not be created and the script gracefully continues, and you won't get an exception.
I'm not sure what exactly happened but I went ahead and reset my migrations once more and when I reseeded it worked. So if anyone else has this problem in the future, just clear the database again and try again.
In my case I had a syntax error when calling Rake :: .....
It worked for me to update the gem with gem 'faker', '~> 2.15', '> = 2.15.1'
Then rails db: migrate: reset and rails db: seed.
I am trying to seed my database, but I am running into this error where its telling me with_deleted is an undefined method in my seeds.rb:
User.with_deleted.each(&:really_destroy!)
User.create(
email: 'admin#example.com',
roles: [:admin],
password: 'abc123',
password_confirmation: 'abc123',
)
I believe it should have been defined from the paranoia gem. Can anyone suggest a workaround? I am working in Rails 5.1.
I tried both of these:
#user = User.create(
email: 'admin#example.com',
roles: [:admin],
password: 'abc123',
password_confirmation: 'abc123',
)
and
User.create(
email: 'admin#example.com',
roles: [:admin],
password: 'abc123',
password_confirmation: 'abc123',
)
and it looked like it worked but when I did a User.last in rails c I still got nil
As far as including act_as_paranoid in my User.rb file, this is what I have:
# acts_as_paranoid
include AddressFields
include MeAssessmentScoring
Is this not enough? But ultimately, can I just get this working without having to involve the paranoia gem? So far, what I have tried above has not worked and when I try to create a user within rails c, I get:
ActiveRecord::RecordInvalid: Validation failed: First name can't be
blank, Last name can't be blank
when I ran this: user = User.create!(email:'admin1#example.com', roles:'admin',password:'abc123', password_confirmation:'abc123')
I was able to get up and running by executing the following:
heroku pg:backups:capture
heroku pg:backups:download
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d thirdelementweb_development latest.dump
after running rake db:reset
i rund rake db:populate and i'm getting this error don't know where it's coming from
rake aborted!
undefined method `+' for 4..5:Range
Tasks: TOP => db:populate
(See full trace by running task with --trace)
in my task folder
namespace :db do
desc " Fill database with sample data"
task populate: :environment do
admin = User.create!(name: "Emple User",
email: "exampel#railstuttorial.org",
password: "password",
password_confirmation: "password")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
email = "example-#{n + 1}#railstutorial.org"
password = "password"
User.create!(name: name, email: email, password: password,
password_confirmation: password)
end
users = User.all(limit: 6)
50.times do
content = Faker::Lorem.sentence(4..5)
users.each { |user| user.microposts.create!(content: content) }
content = nil
end
end
end
You are passing a range to Faker::Lorem.sentence in your 50.times loop.
I'm not sure what you're trying to achieve but that method is expecting an integer (did you mean 4 or 5?) and when it tries to add something to it within the Faker::Lorem code it is causing that error.
I need certain things to be seeded into a test environment, specifically, user roles from CanCan. BUT it seems to load everything under development.
lib/tasks/test _ seed.rake
namespace :db do
namespace :test do
task :prepare => :environment do
Rake::Task["db:seed"].invoke
end
end
end
which correctly pulls in seeds.rb after running bundle exec rake db:test:prepare
db/seeds.rb
admin = Role.create( { name: "admin" }, :without_protection => true)
user = Role.create( { name: "user" }, :without_protection => true)
if Rails.env.production? || Rails.env.development?
admin = User.create!({ name: "Admin",
email: "admin#example.com",
password: "foobar",
password_confirmation: "foobar",
role_ids: 1 },
:without_protection => true)
admin.confirm!
if Rails.env.development?
48.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#example.com"
password = "foobar"
fake = User.create!({ name: name,
email: email,
password: password,
password_confirmation: password },
:without_protection => true)
fake.confirm!
end
end
end
All looks good but then........
$ rails console test
Loading test environment (Rails 3.2.8)
> User.first
User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, email: "admin#example.com" ..................
> Rails.env.development?
=> false
What wrong?
You have to set the environment variable ENV to test before launching task.
Try executing:
$ RAILS_ENV=test rake db:seed
Maybe, you are using the same database for development and test environments. Check your database.yml
First of all, try to execute it in your terminal:
echo $RAILS_ENV
If it is showing test then you have your answer.
If is not, verify your config/database.yml to see if it is using the same database for both environments.
Verify also your config/environment.rb
I'm currently going through the RoR guides, and i'm stuck at...
"Adding following/follower relationships to the sample data."
Here's the code that is suppose to work: sample_app/lib/task/sample_data.rake
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
make_users
make_microposts
make_relationships
end
end
def make_users
admin = User.create!(name: "Example User2",
email: "example2#railstutorial.org",
password: "foobar",
password_confirmation: "foobar")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password)
end
end
def make_microposts
users = User.all(limit: 6)
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end
end
def make_relationships
users = User.all
user = users.first
followed_users = users[2..50]
followers = users[3..40]
followed_users.each { |followed| user.follow!(followed) }
followers.each { |follower| follower.follow!(user) }
end
when i do rake db:reset my database reset with no problems.
when i do rake db:populate an error occurred stating this:
rake aborted!
Validation failed: Follower can't be blank`
so i checked my database, and all tables were populated except for "relationships" table.. any thoughts or suggestions? I'm pretty sure there's a problem with the code, def making_relationships, to be exact. hope anyone has a solution to this..
-Marc
Since you're calling .create! on models like User and Micropost (user.microposts), it is one of them throwing the error mentioned.
Please, post the code for these models to enable us answer more specifically.
You can still debug the problem by yourself though. Just hit rails c in the projects root directory, and try to create instances with the very same attributes you're trying in rake task:
$ rails c
$ user = User.create!(name: name,
email: email,
password: password,
password_confirmation: password)
$ micropost = user.microposts.create!(content: "Hello, cruel world!")
# by this step you should already see some errors raised; if that's not sufficient,
# call the following methods to figure out what model suffers the validation error
user.errors.full_messages
micropost.errors.full_messages
Anyway, it's the validation that's not satisfied. Double check you're passing all required attributes passed when creating a model with a shebang create!. Specifically check which model requires presence of Follower (whatever that is).