Adding devise gem to a Rails 4 / mysql project (table users already exists) - devise

I'm a total newbie with Ruby, Rails, Rake, MySQL (on Ubuntu/bash/command line) and devise (warden 1.2.3, devise 3.0.4). I must continue with what have been done so far : the Rails 4 project is working and linked to a MySQL database (full, populated with users and data). I'm able to change controllers and views.
I must add devise to the project. I'm reading this : https://github.com/plataformatec/devise . My class name is User. So I did rails generate devise User.
When I do rake db:migrate I have this error : Mysql2::Error: Table 'users' already exists: ... of course, since I already have a users table in my project.
Whatever I read ( Devise with Rails 4 ), it's always about create the User model, never how to plug devise on a pre-existing User model.
What document should I read ? Which file I must edit to tell devise "this is my user model and table:" ?

If Ruby/Rails is brand new for you, and you're lost.. (like I was...).
Look for the migration file (migration-name.rb) in "/yourproject/db/migrate/". This file was generated by rails generate devise User (User can by any Model, like UserAdmin or Client ...). This file tells you the modifications Devise will do to the database (what it needs).
Look at this file carefully and make sure that your database match the requirements. If you run rake db:migrate rake will attempt to modify your database. Maybe this is your error (attempt to create a table User, since your table User is already created).
Modify your migration file. You may also change your database manually and delete the migration file (if you don't care to keep track of your migrations); in this last case, of course, you don't have to run the rake command.

Follow the Devise getting-started guide, and intelligently apply the manual steps to your current project where necessary.
https://github.com/plataformatec/devise#getting-started

Related

How to fix Rails Schema file being auto edit after rake db:migrate

We have always had some issues with rails schema file. It got worse after upgraded from rails 3 to rails 4.2. So everytime someone runs "db: migrate" on the local machine, it adds, removes or edit stuff in the schema file. Nothing got affected in the database though.
When we were on Rails3, we got changes like string limit: 255 changes to 100, columns removed and added back in a different row. In Rails 4, apart from the pre-existing ones, we got all the timestamp (created_at, updated_at) added with null: false. We don't set up the default for timestamp in migration files. Also, the index names are changed to those rails generated ones, but we do specify the index names and they are sitting in the database without any issue.
Now it gets really annoying as it causes conflicts on and making noise. Any suggestion to fix this?
User Rails 4.2.10, Ruby 2.5.3, mysql version 5.7.22 by Homebrew.

Rails substitute scaffold User for rails g devise user

I've got a running app (where I created Users through a scaffold) and only now stumble upon the "devise" gem. Now I'd like to include this in my app and manage Users through it. I was thinking that I could maybe drop the db,
rails generate devise User
and substitute model, views, and any other entries that still remain from the Users scaffold before destroying the scaffold all together.
Since this feels pretty error-prone, I'm looking for an easier way to handle this. The thing is that I don't want to rebuild the whole application even though obviously a lot of things rely on the User...
Any suggestions?
No need to create User Model, devise will create it with columns.
So drop User table first and then run following command:
rails generate devise User

Copy Paperclip file attachments from one table to another via pure MySQL commands (not Active Record)

I am supposed to migrate a Paperclip file attachment from a user to a new profile table.
Doing the migration with Active Record takes several hours since the User table is fairly big (400 000 Users).
So I tried to do it via pure MySQL commands inside a Rails migration copying the 4 columns over that are created by the paperclip gem like this:
execute "INSERT INTO profiles
(avatar_file_name,
avatar_content_type,
avatar_file_size,
avatar_updated_at)
SELECT
avatar_file_name,
avatar_content_type,
avatar_file_size,
avatar_updated_at
FROM users"
Checking the columns after the migration, the data was copied correctly and everything seems alright. However, none of the avatars show up and I get the default picture for non existent paperclip attachments.
I was wondering if someone knows how to copy paperclip file attachgment with pure MySQL or has experience in general with migrating large amounts of paperclip file attachements.
Thx,
Uli

Adding columns to a model in a Rails 3 app for a database with the schema dumped from heroku

In my rails app I have a model Events. It has several columns which were created through following this guide https://devcenter.heroku.com/articles/export-from-heroku-postgres and then performing rake db:schema:dump.
Now I want to add some new columns to the Event model. I tried editing schema.rb and restarting the app but that didn't seem to work. Anyone know the proper way to proceed?
----Edit-------
Specifically I added this line to the Event model in schema.rb.
t.datetime "date_time"
When I click the link to add a event in the rails app I receive this error:
undefined method `date_time' for #<Event:0x007fdb59a87118>
The schema.rb file is a representation of the current state of the database schema, it is written by the database migration process, not read.
If you want to add a new column, create a migration:
$ rails generate migration AddNewEventStuff
$ vim db/migrate/add_new_event_stuff*.rb
Then add your columns:
class AddNewEventStuff < ActiveRecord::Migration
def change
    add_column ...
end
end
Everything except the add_column should be there already. Once you have your migration, do a rake db:migrate and you're done. Now you should see some changes in your schema.rb.
See the Ruby on Rails Migration Guide for further details and different ways to build your migrations.
You should try heroku run rake db:schema:load. That said, it's probably the wrong way to go about. Instead, you should make changes to your schema via migrations, and then run a heroku run rake db:migrate on it.

How to do database migration Rails3 + datamapper

I used dm-rails gem that allows datamapper to hook into rails-3 ,generated a scaffold and a migration file ,did rake db:migrate for database migration but nothing happens no error no migration, can any one suggest me how to run migrations with datamapper and rails-3.
You have to get dm-rails that add among other things, 2 specific rake tasks:
rake db:automigrate
it performs destructive automigration, so it checks if table exists, and if so then drop it, and create one more time.
and
rake db:autoupgrade
It's upgrade your database schema to match your model properties.