uninitialized constant Rails::Generator::GeneratedAttribute::ActiveRecord - ruby-on-rails

Using rails 2.3.5 when I tried to run the command : script/generate scaffold user user_name:string email:string, I get the error:
uninitialized constant Rails::Generator::GeneratedAttribute::ActiveRecord
I have searched for this error and I found the only solution is to uncomment this line in my environment.rb
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
which is already uncommented in my environment. What can I do?

a newbie's fault :) , i came from rails 3 , and i have installed bundle to be able to install gems in my rails 2 project but i wasn't know that i should comment the line above again in case of using bundler as it wasn't mention in the instruction of adding bundler.
after commenting # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
then i got the following
➜ ~ script/generate scaffold user user_name:string email:string
exists app/models/
exists app/controllers/
exists app/helpers/
exists app/views/users
exists app/views/layouts/
exists test/functional/
exists test/unit/
exists test/unit/helpers/
exists public/stylesheets/
overwrite app/views/users/index.html.erb? (enter "h" for help) [Ynaqdh] y
force app/views/users/index.html.erb
create app/views/users/show.html.erb
create app/views/users/new.html.erb
create app/views/users/edit.html.erb
create app/views/layouts/users.html.erb
create public/stylesheets/scaffold.css
create app/controllers/users_controller.rb
create test/functional/users_controller_test.rb
create app/helpers/users_helper.rb
create test/unit/helpers/users_helper_test.rb
route map.resources :users
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/user.rb
create test/unit/user_test.rb
create test/fixtures/users.yml
create db/migrate
create db/migrate/20130820214343_create_users.rb
and rake db:migrate is done, so that means now everything is fine ?

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

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

rails generate migration error mongo_mapper not found

Follow Installation in Rails 3 of Mongo Mapper i add to config/application.rb file :
config.generators do |g|
g.orm :mongo_mapper
end
when rails generate migration my_migration i got :
error mongo_mapper [not found]
but when rails generate model my_model, i got a fine model class with include MongoMapper::Document and this output :
invoke mongo_mapper
create app/models/user.rb
invoke test_unit
create test/unit/user_test.rb
create test/fixtures/users.yml
You don't have migrations while working with MongoDB.
Because MongoDB is a schema less database.
That's the reason you can't generate migrations with mongo mapper.

Rails - uninitialised constant AdminUser with ActiveAdmin gem

I'm new in Rails and I'll try to install ActiveAdmin to my existing project (Rails 3.2.1).
i've had activeadmin, meta_search, devise and sass-rails in my gemfile. Require devise in the configuration/application.rb.
When I try to run "rails generate active_admin:install" or "rails server", i have an error :
C:/RailsInstaller/Ruby1.9.3/l
ive_support/inflector/methods
connstant AdminUser (NameError)
I dont know how to fix.
Thanks :)
Just delete the following line on routes.rb
devise_for :admin_users, ActiveAdmin::Devise.config
Then rerun
rails g active_admin:install
I got this same error after uninstalling and resintalling activeadmin (as I wanted to remove it from admin_user to install it in my own user).
Solution was to search through my project for "admin", and comment all the code that activeadmin had left after uninstall.
The files where I found activeadmin code were:
routes.rb
schema.rb

Using Rails Inflections with `rails generate`

I'm trying to generate a model called ClassAttendance, but Rails keeps naming the migrations class_attendances. I've tried correcting this problem by placing the following code the following code in \config\initializers\inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable "attendance"
end
This seems to work fine in the rails console:
$ rails console
Loading development environment (Rails 3.2.6)
irb(main):001:0> "attendance".pluralize
=> "attendance"
Unfortunately, the rails model generator seems to be unaffected:
$ rails generate model ClassAttendance
invoke active_record
create db/migrate/20120806201910_create_class_attendances.rb
create app/models/class_attendance.rb
invoke rspec
create spec/models/class_attendance_spec.rb
Does it have something to do with this?
irb(main):002:0> "class_attendance".pluralize
=> "class_attendances"
Or is there some other problem I'm not seeing?
That is the workaround, you need to place it in the inflections.rb file in the config/initializers/. So your config/initializers/inflections.rb would be
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( attendance class_attendance ClassAttendance)
end

Resources