NoMethodError: undefined method reset_sent_at hartle tutorial - ruby-on-rails

I'm doing chapter 10 of enter link description here. I got the same error mentioned in enter link description here
PasswordResetsTest#test_password_resets:
NoMethodError: undefined method `reset_sent_at=' for #<User:0xccd47c0>
app/models/user.rb:66:in `create_reset_digest'
app/controllers/password_resets_controller.rb:12:in `create'
test/integration/password_resets_test.rb:17:in `block in <class:PasswordResetsTest>'
I try to do everything mentioned in the answer.
first thing I did was:
rails generate migration add_reset_to_users reset_digest:string reset_sent_at:datetime
and the answer was:
Another migration is already named add_reset_to_users:
so I'm sure I did the migration before.
This _add_reset_to_users.rb file in migrate folder.
class AddResetToUsers < ActiveRecord::Migration
def change
add_column :users, :reset_digest, :string
end
end
Then I try to restart my rails server.(I'm not sure if I'm doing it right) using
rails server
and then shutting down the server.
Non of them worked. I'm still getting the same error.

Another migration is already named add_reset_to_users:
You had already created the migration previously, as you noted, but this migration did not include the addition of the reset_sent_at column.
At this point the easiest thing to do would be to create a new migration to add the missing column.
rails generate migration add_reset_sent_at_to_users reset_sent_at:datetime

Related

NameError: uninitialized constant Model - rake db:migrate

I'm trying to migrate a change to my Comments table with rails migration. I've already made the migration and everything looks good but when I run rake db:migrate it sends back this error NameError: uninitialized constant Model. I can't see the problem? All the naming look right to me. Here is my migration file let me know if anything looks off or if you need more information. Thank you!
MIGRATION:
class AddColumnToComments < ActiveRecord::Migration
def change
add_column :comments, :fav_drink, :string
add_column :comments, :visit_time, :string
end
end
ERROR:
I assume you've used rails generator to create your migration and this generator has probably create a migration file named: 201606xxxxxxxx_model.rb.
If it's the case, rename you migration file as follow: 201606xxxxxxxx_add_column_to_comments.rb

Removing column is not working

I am trying to remove a column from my database. I'm running this
rails generate RemoveOccupancyFromSpaces occupancy:string
But then am getting this error message
Could not find generator 'RemoveOccupancyFromSpaces'. Maybe you meant 'resource_route' or 'devise:controllers' or 'active_admin:page'
Run `rails generate --help` for more options.
Where am I going wrong here?
The correct syntax is rails generate migration RemoveOccupancyFromSpaces occupancy:string
This should yield a file with the correct syntax:
class RemoveOccupancyFromSpaces < ActiveRecord::Migration
def change
remove_column :spaces, :occupancy, :string
end
end
Try rails generate migration RemoveOccupancyFromSpaces occupancy:string

Heroku rake db:migrate failing - uninitialized constant

My app is working fine locally and my push to Heroku was successful. But, when I run heroku run rake db:migrate, I get the following error:
NameError: uninitialized constant AddWeightToExercises
Here is the failed migration:
class AddWeightToExercise < ActiveRecord::Migration
def change
add_column :exercises, :weight, :float
end
end
edit: Thanks for the help everyone. The solution was to pluralize the class name to match the file name. Thanks for the help and quick responses.
Your migration file's name should correspond to AddWeightToExercises. It should be accordingly xxxxxxxx_add_weight_to_exercises, where xxxxxxx corresponds to a particular timestamp.
Your file name uses "exercises" plural, but your class name is AddWeightToExcercise singular. The two need to be consistent for rails to dynamically load the appropriate class.

Revert specific, old migration in Rails without reverting later migrations

I started out using Rolify in a Rails app and created a migration to set up its tables about 15 migrations ago. I've now decided to replace it with my own code and want to revert that one migration without touching all of the later migrations. The database is in use now, so reverting 15, removing the one I don't want to add and then applying the subsequent 14 would destroy data.
Section 3.11 of the Rails Guide on migrations suggests that this can be done by creating a new migration which reverts the specific old migration by name:
class FixupExampleMigration < ActiveRecord::Migration
def change
revert ExampleMigration
create_table(:apples) do |t|
t.string :variety
end
end
end
I tried to customise this to my context, which would look like this:
class RolifyDestroyRoles < ActiveRecord::Migration
def change
revert RolifyCreateRoles
end
end
(The first line of my original Rolify migration was class RolifyCreateRoles < ActiveRecord::Migration). However, I get a namespace error:
StandardError: An error has occurred, this and all later migrations canceled:
uninitialized constant RolifyDestroyRoles::RolifyCreateRoles/home/slack/rails/tracker/db/migrate/20150127093921_rolify_destroy_roles.rb:3:in `change'
Maybe something has changed in Rails 4. Does anyone know how I should refer to RolifyCreateRoles so that Rails can find it?
Reverting a specific migration in rails :
Let's say we have a migration :
db/migrate/20150127071749_create_users.rb
revert:
rake db:migrate:down VERSION=20150127071749
setup again:
rake db:migrate:up VERSION=20150127071749
Hope that helps :)

ruby on rails rollback database created problems

I completely rolled back my migration from the beginning so I can add a few columns, now when I'm trying to create new user I get a
NoMethodError in Devise::SessionsController#create
undefined method `image_changed?' for #<User:0x007fa99ea152f8>
I'm using devise and I have carrierwave. My application worked, and I had images before, but when I rolled back and deleted the images from the upload folder, I get this undefined method.
Does anyone know where this is coming from? I'm completely clueless,
Thanks
I believe that inside your user model you may have carrierwave still mounted to your model.
class User < ActiveRecord::Base
mount_uploader :image, ImageUploader
#mounts the uploader to the given column in this case which it is :image
end
The error undefined methodimage_changed?' for #` indicates that you must be missing some sort of column fro your model.
1) rails g migration AddImageToUsers image:string
2) Run rake db:migrate

Resources