NoMethodError: undefined method `with_deleted` - ruby-on-rails

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

Related

active_admin login failure after deploying succed

I use active_admin in local environment and it works fine.
I can login as adminuser after executing "rake db:seed".
(here is my seed.rb file)
AdminUser.create!(email: '1#1', password: '123456', password_confirmation: '123456') if Rails.env.development?
However in production environment, I can't access to active_admin page as admin_user.
I already tried "rake db:seed RAILS_ENV=production" but it's not working.
(here is my seed.rb file in production mode)
AdminUser.create!(email: '1#1', password: '123456', password_confirmation: '123456') if Rails.env.production?
(here is my logs)
Anyone plz help thanks!

"Email has already been taken" error on db:seed

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.

why heroku can't run dev:fake_user?

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

How to configure Cucumber in Rails 4

I'm trying to use Cucumber in a Rails 4. I've added the Cucumber-Rails gem, followed the steps in the instructions but when I wrote step definitions like so:
When(/^I submit a sign up with the following:$/) do |table|
user = User.create({
first_name: 'Name',
last_name: 'Last',
email: 'email#example.com',
domain: 'example.com',
password: 'foobar',
password_confirmation: 'foobar'
})
end
I get the following error: uninitialized constant User (NameError)
./features/step_definitions/users/sign_up_steps.rb:2:in/^I submit a sign up with the following:$/'
features/users/sign_up.feature:4:in When I submit a sign up with the following:'
What am I missing?
Please check , it's looking like you have missed the User model
User model is missing for which you are running tests.

Add User To User Table In Terminal Rails 4 Postgresql

I am using Rails 4 and Devise 3 and Postgresql for my database. How can I enter a user through the terminal into my user table?
rails db
is how I enter into the database.
$ rails console
rails > User.create email: 'john#example.com', password: 'password', password_confirmation: 'password'

Resources