Test fails when adding ":remember_token" - ruby-on-rails

I'm following chapter 8 of Michale Hartl's tutorial. When I add:
it { should respond_to(:remember_token) }
to the user_spec.rb file the test fails, with the failure notice pointing directly to this one line in the file.
Prior to adding this line, all tests past.
The only other steps I took after adding this line (and before running the test) was to run a remember token:
$ rails generate migration add_remember_token_to_users
I then updated db/migrate/[timestamp]_add_remember_token_to_users.rb as follows:
class AddRememberTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :remember_token, :string
add_index :users, :remember_token
end
end
and development and test databases as usual:
$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare

As Spork caches the rails environment it does not know about the migration until it is restarted.
Therefore you need to restart the Spork server so that it will reload the rails environment including the new migrations.

Related

Obstacle with Ruby Rails Authentication

I am a brand new Ruby on Rails User
When I enter localhost:3000 ,
I get an error reading
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=development
You have 2 pending migrations:
20221119205559_add_devise_to_users.rb
20221119211811_drop_users_table.rb
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
end
def load_schema_if_pending!
In a nutshell, it was working before but I attempted to create a a basic authentication page(which was also working) , but for some reason when I clicked sign up I received an error also.
Thank you for any tips on how to fix this!
I have tried to
rake db:drop
rake db:create
rake db:migrate
Editing the migrate file with:
edit your migration file
class DropUsersTable < ActiveRecord::Migration
def change
drop_table :users
end
end
Then doing rake db:migrate
Also, I have run :
rails generate devise:install
rails generate devise User
bundle exec rake db:migrate

bundle exec rake db:migrate worked, but development.sqlite3 is empty

I'm new to RoR. The following line executed normally:
bundle exec rake db:migrate
Output showed that it is migrated. development.sqlite3 and production.sqlite3 were created but empty, although schema.rb was written properly. The users table was successfully created, but without any records. Also I executed:
bundle exec rake db:test:prepare
test.sqlite3 is created and also written properly. This is the content of the file inside the migration file:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
Kindly help me solve the problem. Let me know if any other details need to be specified.
So bundle exec rake db:test:prepare probably sets your environment to test.
That would be the reason why you'll end up with a working test database.
If you want to have the data in the development database, you need to set the environment to development.
Try RACK_ENV=development bundle exec rake db:test:prepare.
Although the cleaner solution is to create a seed file. See: http://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data

What to do after a failed heroku db:rollback

I'm at the very beginning stages of learning rails using heroku as deployment tool. I ran into a bit of problem today, which is now fixed, but I was wondering if there's a proper/better way to do what I did.
My problem was as follows: I wrote a migration file that created a table with some indices (using add_index). The code would look like this:
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :username
...
end
add_index :users, :username, :unique => true
end
def self.down
drop_table(:users)
remove_index :users, :username
end
end
heroku run rake db:migrate ran fine but heroku run rake db:rollback failed because (I assume) remove_index was trying to delete an index from a column that had already been erased.
So I then added a self.down method to my migration file (removing the indices before dropping the table). Afterwards, heroku run rake db:migrate didn't do anything, and heroku run rake db:rollback is stuck at the same error as before. Resetting the database or dropping the table didn't work either. I ended up removing the add_index lines in my migration before the rollback finally works.
... and unfortunately I no longer have any idea why db:rollback failed. The error message was 'index_users_on_username' on table 'users' does not exist', so my guess is that I did something stupid like modifying the database or modified the migration file before doing a rollback. Or could it be because I am mixing change and down method in the same migration file?
Anyway, my main question is, if a db:rollback fails, what then?
Some options in my head:
Fix the migration file until the rollback works
Fix the database manually until the rollback works
Fix the database manually and ignore the migration completely (dunno how to do this)
this)
???

new to rails, setting up db then running rake db:create/migrate

hi im currently learning rails, and following a tutorial. the instructions were to edit the migration file after i've created the app, then running rake db:migrate, then rake db:create.
i've edited the migration file to this:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :encrypted_password
t.string :salt
t.timestamps
end
end
end
then when i've run 'rake db:migrate' i got an error
Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` ...
after i'm supposed to run 'rake db:create', then im getting this
user_auth_development already exists
user_auth_test already exists
You run rake db:create once and only once, and you run it first. Then you run rake db:migrate every time you add/change a migration. You've either already run this migration, or you are pointing at a database that already exists and already contains a table named users. My guess is that you ran the migration once already, in which case you're probably good to go. If you want to nuke the DB and start over, do rake db:drop db:create db:migrate.
We can simply give, it will do all the rake task which is require for database creation and migration
rake db:setup
For Rails 5 and 6, the command is:
rails setup
This will "create the database, load the schema, and initialize it with the seed data" (docs).
For rails 6 & above, you can give this command to create a database, migrate all the migration files, and seed the data into the database:
rails db:prepare

rails migration version issue: any new migration not working

From this morning, I am facing weird issues with Rails devise. Following is output of my ls and rake db version command.
hrishikesh#hrishikesh-ubuntu:~/git-public/personaldiary/db/migrate$ ls -1
20120110083934_devise_create_users.rb
20120110090514_create_posts.rb
20120110090845_add_user_id_to_post.rb
20120203035323_add_confirmable_to_devise.rb
20120203035323_add_confirmable_to_devise.rb~
20120203043601_add_lockable_to_devise.rb
20120203043601_add_lockable_to_devise.rb~
hrishikesh#hrishikesh-ubuntu:~/git-public/personaldiary/db/migrate$ rake db:version
(in /home/hrishikesh/git-public/personaldiary)
DEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /usr/lib/ruby/vendor_ruby/activerecord.rb:2)
Current version: 20120203034555
hrishikesh#hrishikesh-ubuntu:~/git-public/personaldiary/db/migrate$
If I try to add any new migrations, rake db:migrate throws error that tells me that some column already exists, and fails.
My failing migration code is here:
class AddConfirmableToDevise < ActiveRecord::Migration
def change
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
I specifically do not want to use up and down methods because of this
Please help.
After spending hours to find solution, I decided to give up and ran
rake db:migrate:reset
And it worked, only thing is my data was lost, which was not that big deal at this point.
Thank you everyone for attempting to solve this.

Resources