Spree_auth_devise seed fails with Unknown column 'roles.name' - ruby-on-rails

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

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

acts-as-taggable-on gem -- "taggings_count" does not exist

After upgrading the gem acts-as-taggable-on and applying the migrations:
rake acts_as_taggable_on_engine:install:migrations
rake db:migrate
I keep having the errors in my tests:
ActiveRecord::StatementInvalid:
PG::UndefinedColumn: ERROR: column "taggings_count" does not exist
LINE 1: UPDATE "tags" SET "taggings_count" = COALESCE("taggings_coun...

Don't know how to build task 'db:seed_fu'

I have just added the seed-fu gem to my app for seeding my test-database:
group :test do
gem 'seed-fu'
end
I made a custom rake task (in /lib/tasks/db.rake) for seeding only my test-database:
namespace :db do
desc "seed_fu only in test-database"
task seed_fu_test: :environment do
Rails.env = 'test'
puts "Seeding will be made in test-base ONLY!"
Rake::Task["db:seed_fu"].execute
end
end
If I do rake -T | grep seed then my new custom-made task is shown amongst other seed-tasks:
rake db:seed # Load the seed data from db/seeds.rb
rake db:seed_fu # Loads seed data for the current environment
rake db:seed_fu_test # seed_fu only in test-database
Now when I do rake db:seed_fu_test I get
rake aborted!
Don't know how to build task 'db:seed_fu'
But when I do
rake db:seed_fu RAILS_ENV='test'
then seed_fu seeds my test-database well.
Figured it out- the problem was in my Gemfile. Because I added the seed-fu gem into test-group then in development-environment, which was my default for running also the rake db:seed_fu_test task, the seed_fu gem was not seen.
Therefore when moving gem 'seed-fu' line into my :development-group in Gemfile, the problem was solved.

Rails Heroku - Resetting the database

I am trying to reset my database.
It isn't working locally or in heroku.
I succuessfully ran each of these commands:
1. rake db:drop
2. rake db:create
3. rake db:migrate
The migrations took a while to succeed. I commented out the ones that were causing a problem and the whole job finishes migrating.
I then try to reset my database with:
4. rake db:reset
I get this error:
initialize_schema_migrations_table()
-> 0.0031s
rake aborted!
NoMethodError: undefined method `name=' for #<University:0x007fc288bdcca0>
/app/vendor/bundle/ruby/2.2.0/gems/activemodel-4.1.9/lib/active_model/attribute_methods.rb:435:in `method_missing'
I can't find anywhere in the code base that has a method called 'name' for university. I have run searches looking for university.name and name near university.
I have updated my gems and run bundle install.
What does this error message mean?
When I try:
rake db:reset --trace
I get:
** Execute db:abort_if_pending_migrations
rake aborted!
NoMethodError: undefined method name=' for #<University:0x007f9a1b24da30>
/Users/em/.rvm/gems/ruby-2.2.2/gems/activemodel-4.1.9/lib/active_model/attribute_methods.rb:435:inmethod_missing'
I have run all migrations and refreshed rake db:migrate
This is the error you get when you are trying to write to a variable that you haven't declared an attr_writer for.
class Foo
attr_reader :bar
def initialize
#bar = 1
end
end
> f = foo.new
=> #<Foo:0xa22ef0c #bar=1>
> f.bar
=> 1
> f.bar = 2
NoMethodError: undefined method `bar=' for #<Foo:0xa22ef0c #bar=1>
from (irb):23
from /usr/local/rvm/rubies/ruby-2.1.3/bin/irb:11:in `<main>'
It looks like you've got something in your seeds.rb file causing the error.
The reason you're seeing the issue when running rake db:reset but not when running those 3 individual steps is because rake db:reset doesn't run those 3 individual steps.
rake db:reset will run the following:
rake db:drop
rake db:setup
and subsequently, rake db:setup will run these:
rake db:create
rake db:schema:load
rake db:seed
If you only want to do the initial 3 steps (rake db:drop, rake db:create, rake db:migrate), you can run this instead:
rake db:migrate:reset

Active_admin running an error when run rake db:migrate

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

Resources