Active_admin running an error when run rake db:migrate - ruby-on-rails

I'm trying to install ActiveAdmin under Rails 4 to generate my admin panel.
I added the gem and installed with the below commands:
gem 'activeadmin', github: 'gregbell/active_admin'
bundle install
rails g active_admin:install # creates the AdminUser class
rails g active_admin:install User # uses an existing class
But when I try to migrate I get an error:
$ rake db:migrate
== AddDeviseToAdminUsers: migrating ==========================================
-- change_table(:admin_users)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "admin_users" ADD "email" varchar(255) DEFAULT '' NOT NULL/usr/local/rvm/gems/ruby-2.0.0-p247/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
As mentioned in issue 753 on github I changed the AddDeviseToAdminUsers migration from change_table to create_table but that results in this error:
== AddDeviseToAdminUsers: migrating ==========================================
-- create_table(:admin_users)
rake aborted!
Can anyone help please?

The exception you're seeing is due to a migration conflicting with your existing database structure. Your admin_users table already contains an "email" column, which is why you're seeing the error duplicate column name: email.
You should only run the active_admin:install generator once. Running the ActiveAdmin setup with a clean application should only involve the following:
# Add the BETA gem with Rails 4 support. The ActiveAdmin master
# branch is still in heavy development.
gem 'activeadmin', github: 'gregbell/active_admin'
# Bundle
bundle install
# Setup ActiveAdmin
rails g active_admin:install
For more advanced cases, where you already have an ActiveRecord model for an admin user then you'd use this variant of the generator: rails g active_admin:install MyAdminUser

Related

How to undo the action of rails db:migrate in Rails 5.0.0.1

When migrating the database, I made a spelling mistake.
I want to generate a scaffold by running:
rails generate scaffold Micropost context:text user_id:integer
rails db:migrate
Although I made a mistake by leaving out the colon when I ran:
rails generate scaffold Micropost context:text user_id integer
rails db:migrate
I want to undo this migration, how to do it?
(I'm using Rails 5.0.0.1)
When I run rails db:migrate, I get an error of:
SQLite3::SQLException: table "microposts" already exists:
When I run rails db:migrate:status, I get the following output:
Status Migration ID Migration Name
up 20161024021157 Create users
up 20161024025545 ********** NO FILE **********
down 20161024025805 Create microposts
I tried to use rails db:migrate:down VERSION=20161024025805. There wasn't any message showing in the command line. Then I ran rails db:migrate. The error is the same.
rails db:rollback will simply rollback one migration which I believe is what you are looking for
For a more specific rollback, you can run rails db:migrate:down VERSION=numberofversion
Replace the numberofversion with the version number of the migration file generated, for example:
rails db:migrate:down VERSION=1843652238
Edit:
Since you are getting the error that the Microposts table already exists, you must follow these steps to remove the table:
Run rails generate migration DropMicroposts
Go to the /db/migrate folder and find the latest migration file you just created
In that file paste this:
class DropMicroposts < ActiveRecord::Migration
def up
drop_table :microposts
end
end
run rails db:migrate
After this, run rails generate scaffold Micropost context:text user_id:integer
Then run rails db:migrate

Spree_auth_devise seed fails with Unknown column 'roles.name'

I am installing Spree in conjunction with spree_auth_devise. I am running into a problem when creating an admin user either through seeding or by running the command bundle exec rake spree_auth:admin:create
Here is the full error message:
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'roles.name' in 'where clause': SELECT COUNT(DISTINCT `spree_users`.`id`) FROM `spree_users` LEFT OUTER JOIN `spree_roles_users` ON `spree_roles_users`.`user_id` = `spree_users`.`id` LEFT OUTER JOIN `spree_roles` ON `spree_roles`.`id` = `spree_roles_users`.`role_id` WHERE `roles`.`name` = 'admin'
I have followed these steps to install spree_auth_devise and can confirm the migrations are created and run successfully:
bundle exec rake spree_auth:install:migrations
bundle exec rake db:migrate
bundle exec rails g spree:auth:install
I receive the error as a result of running this line in seeds.rb:
Spree::Auth::Engine.load_seed if defined?(Spree::Auth)
I receive the same error when running:
bundle exec rake spree_auth:admin:create
I have also followed these intructions:
At this point, if you are using spree_auth_devise you will need to
change this line in config/initializers/spree.rb:
Spree.user_class = "Spree::LegacyUser"
To this:
Spree.user_class = "User"
My problem was pretty simple in the end. I included spree_auth_devise when I shouldn't have. Here are the correct steps:
gem 'spree', github: 'spree/spree', :branch => '2-3-stable'
$ bundle install
$ rails g spree:install --migrate=false --sample=false --seed=false
config/initializers/spree.rb by changing this line: Spree.user_class = "Spree::User" to this: Spree.user_class = "User"
$ rails g spree:custom_user User
$ rake db:migrate

RAILS : undefined method `secret_key=' for Devise:Module

I defined a model in rails using command rails generate model testdetails . After that I went to db/migrate and wrote the fields for this model
class CreateTestDetails < ActiveRecord::Migration
def self.up
create_table :test_details do |t|
t.column :TestName ,:string
t.column :TestType ,:integer
end
end
end
then i did db:migrate , it throws some error . I google it and found out that it may be coming due to devise version ( I am using devise for authentication) , I updated Gemfile and wrote the version of devise (2.1) and did bundle install . After that I again did db : migrate but it is showing this error
rake aborted!
undefined method secret_key=' for Devise:Module
/home/vibhor/rails_projects/recruit/config/initializers/devise.rb:7:inblock in '
/home/vibhor/rails_projects/recruit/config/initializers/devise.rb:3:in <top (required)>'
/home/vibhor/rails_projects/recruit/config/environment.rb:5:in'
Tasks: TOP => db:migrate => environment
what should i do so that this model can be created without any error? i am using rails 3.2.13 and ruby 2.0.0
I think this is due to gem version so update it to latest version 3.x or remove that line from your config/initializers/devise.rb file.
In your config/initializers/devise.rb file add this line:
config.secret_key = 'Your secret Key'
And use rake secret to generate your secret key.
There's an issue open in github if you need more info.
Had the same problems lately. Check devise initializers, as it has changed lately. For me it solved the problem.

Rails migration error database_authenticatable

-- create_table(:admin_users)
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `database_authenticatable' for #
Tasks: TOP => db:migrate
How to solv it? Thanx!
migration
create_table(:admin_users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
t.timestamps
end
have all gems in gemfile and installed
Make sure you have devise in the Gemfile and the bundle is installed.
Answer is simple device team sucks!!!
to solve this need make cnanges in GEMFILE
gem 'devise', "~> 1.5"
because in 1.5 there is database_authenticatable type support and in 2.1.0 there is support of only compatibility not creation of fields with this type
thanx everybody.
If you're just getting started with devise (vs updating from previous verions), you might have missed the following step before doing rake db:migrate
rails generate devise:install
This creates
create config/initializers/devise.rb
create config/locales/devise.en.yml
which define the method rake is complaining about above.
Source:
https://github.com/plataformatec/devise
With Devise 2.0 and newer, the migration helper methods (t.database_authenticatable for example) are not available (as stated on the wiki here ) If you're making a new model for users, just use the devise migration generator like so:
rails g devise admin_users (If you're installing devise on your app)
If you're adding the required fields to an existing user model, you should check this page on the devise wiki.
Check out the main README for devise, which has up-to-date information for installing the latest version of devise on Rails.

Ruby on Rails - sqlite 3 rake migrations not updating the database

I am using RoR (3.2.2) and SQLite 3 (1.3.5). When I initially generate a model I am able to successfully create a database. However, whenever I try to use the migration generator it appears to not have any issues in the command line (no errors), but when I check the database nothing has updated or changed.
For example, I create this database:
$ rails generate model User name:string email:string
db/migrate/[timestamp]_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
And I run a migration:
$ bundle exec rake db:migrate
So far so good, I check my database using SQLite Database Browser and everything looks as it should.
Then if I want to add an index:
$ rails generate migration add_index_to_users_email
db/migrate/[timestamp]_add_index_to_users_email.rb
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
I run a migration:
$ bundle exec rake db:migrate
And command line gives me the following:
bundle exec rake db:migrate
== AddIndexToUsersEmail: migrating ===========================================
== AddIndexToUsersEmail: migrated (0.0000s) ==================================
However, when I check my database using SQLite Database Browser it isn't updated. I get the same results if I try to add new columns to the table, etc. The only way I have been able to do migrations is manually updating the database using the SQLite Database Browser. Any help as to why it is not working through Rails and the command line would be greatly appreciated.
Here is my gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.2'
gem 'bootstrap-sass', '2.0.0'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.8.1'
gem 'annotate', '~> 2.4.1.beta'
end
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
gem 'capybara', '1.1.2'
end
group :production do
gem 'pg', '0.12.2'
end
Database.yml
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
Example of successfully adding a column:
rails generate migration add_password_digest_to_users password_digest:string
invoke active_record
create db/migrate/20120318235656_add_password_digest_to_users.rb
$ subl db/migrate/[timestamp]_add_password_digest_to_users.rb
$ bundle exec rake db:migrate
== AddPasswordDigestToUsers: migrating =======================================
-- add_column(:users, :password_digest, :string) -> 0.0008s
== AddPasswordDigestToUsers: migrated (0.0009s) ==============================
Example of unsuccessfully adding a column:
$ rails generate migration add_remember_token_to_users
invoke active_record
create db/migrate/20120319010623_add_remember_token_to_users.rb
$ subl db/migrate/[timestamp]_add_remember_token_to_users.rb
$ bundle exec rake db:migrate
== AddRememberTokenToUsers: migrating ========================================
== AddRememberTokenToUsers: migrated (0.0000s) ===============================
Notice when it fails to update the database the migration time is zero. I'm not sure what I am doing wrong. Thanks in advance for any suggestions.
I solved the problem undoing the last migration with
rails destroy migration add_index_to_users_email
Then I closed my terminals to ensure that all db connections were closed, then I run
rake db:reset
Finally, running the same commands again
rails generate migration add_index_to_users_email
Updating the file with the same content and running
rake db:migrate
All the same commands and files that were not working before now worked at all (for me).
I hope this works for you as well.
I think what happens is you get the syntax slightly wrong (it's insanely tricky) then you get an empty migration which doesn't do anything.
I would double check that the actual migration file has add column. I often end up manually putting the actual change (or up/down) in.
As t Harrison says, adding an index is more subtle - as you sure it doesn't get added if you refresh the sqlite borwser. The examples you gave that worked... are to add columns...
The output you posted from the add index example shows that the migration is indeed running ... but you report it's not actually doing anything. Try bundle exec rake:db rollback (with optional STEP=n to go more than one step back). This really should "just work", and a case of an index might be a little subtle -- try adding a column or something (which you said you have done, but, just to be sure). Does it really not work? How can you tell?

Resources