Ruby on Rails Import CSV into MySQL - mysql

Newbie question....
Trying to start a project in rails. I have different spreadsheets in csv format I'd like to import into the MySQL database to be able to manipulate the data.
After looking around on stackoverflow, Google, etc. I wrote a rake task requiring fastercsv to do the job. I keep getting errors so hopefully you can help.
...
Ok so I changed the code to use 'csv' vs 'fastercsv'...still getting errors. See below
New Code for Rake File (take 3):
require 'csv'
desc "Import gac from csv file"
task :import => [:environment] do
file = "gac.csv"
CSV.foreach(file, :headers => true) do |row|
Institution.create({
:institution_name => row[0],
:website => row[1],
:email => row[2],
:category_1 => row[3],
:category_2 => row[4],
:category_3 => row[5],
:category_4 => row[6],
:category_5 => row[7],
:category_6 => row[8],
:category_7 => row[9],
:category_8 => row[10],
:category_9 => row[11],
:category_10 => row[12],
:category_11 => row[13],
:institution_description => row[14]
})
end
end
Error Codes:
Daves-MacBook-Pro:vendor dave$ rake import --trace
** Invoke import (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute import
rake aborted!
invalid byte sequence in UTF-8
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1855:in `sub!'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1855:in `block in shift'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1849:in `loop'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1849:in `shift'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1791:in `each'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1208:in `block in foreach'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1354:in `open'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1207:in `foreach'
/Users/dave/rails_projects/vendor/lib/tasks/import.rake:8:in `block in <top (required)>'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/bin/rake:19:in `load'
/Users/dave/.rvm/gems/ruby-1.9.3-p0/bin/rake:19:in `<main>'
Tasks: TOP => import

lib/tasks/import.rake
require 'csv'
desc "Import gac from csv file"
task :import => [:environment] do
file = "vender/gac.csv"
CSV.foreach(file, :headers => true) do |row|
Putthemodelnamehere.create ({
:columnnamewhatever => row[1],
:columnname => row[2],
:columnname => row[4]
})
end
end
Then just run rake import or bundle exec rake import. Hope this helps (this isn't using fastercsv but this is the solution I'd recommend.)

Make sure that you have the Institution model class defined in app/models and/or make sure that you have run the db migration(s) (rake db:migrate) to create the institutions table - assuming you used a generator (or scaffold) to create the model.

The errors you are getting are probably because there is something in your csv that is not properly encoded for UTF-8.
One way of dealing with that would be to force the encoding. You could do the following:
For each association you have above, do this instead:
:category_1 => row[3].encode("UTF-8", replace: ' '),
Hopefully that will work for you.

Related

"uninitialized constant" error during migration

I'm a newbie in Ruby on Rails and I want to generate a mysql database using migrations.
I tried this command
ruby bin/rake db:drop db:create db:migrate --trace
but I always get this error:
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
uninitialized constant CreateOrganisation::Referential
C:/Sites/blog/db/migrate/20120823053740_create_organisation.rb:13:in `up'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:598:in `exec_migration'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:578:in `block in migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with
_connection'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:577:in `migrate'
C:in `migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:991:in `block in execute_migration_in_transacti
on'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1039:in `ddl_transaction'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:952:in `block in migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (req
uired)>'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
bin/rake:4:in `<main>'
Tasks: TOP => db:migrate
I tried the commands:
rake db:rollback
rake db:reset
rake db:schema:dump
rake db:schema:load
but in vain.
The file that causes the error (I guess) is:
class CreateOrganisation < ActiveRecord::Migration
def up
create_table :organisations do |t|
t.string :name
t.timestamps
end
change_table :referentials do |n|
n.belongs_to :organisation
end
change_table :users do |u|
u.belongs_to :organisation
end
Referential.reset_column_information
User.reset_column_information
organisation = Organisation.find_or_create_by_name!("Chouette")
Referential.update_all :organisation_id => organisation.id
User.update_all :organisation_id => organisation.id
end
def down
drop_table :organisations
end
end
I tried putting def change..end instead of def up..end/def down..end but I still get the same error
I can't see what I'm missing here.
You're trying to change the table referential and it doesn't exist:
change_table :referentials do |n|
n.belongs_to :organisation
end
The error refers to an uninitialized constant ":referentials", which refers to that particular table.
Consider using a generator to create your migrations until you understand how they work; you can always tweak the generated migrations or write your own from scratch later.
Example
rails generate migration CreateFoo name:string amount:integer owner:string:index
And that will create
class CreateFoo < ActiveRecord::Migration
def change
create_table :foos do |t|
t.string :name
t.integer :amount
t.string :owner
end
add_index :foos, :owner
end
end
Reference
I suggest you read the Rails Migration Guide, which should help you understand how to use migrations.

ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`

I am trying to implement a "comment" feature as part of my assignment for an project I am building.
Earlier in the course we created a comment table and had the Faker gem generate fake comments.
My instructions are as follows:
Comments must be associated with users, so add a user_id foreign key to the comments table. Remember to add an index to it too;
Update the User model so you can call user.comments, and the Comment model so you can call comment.user;
Modify the seeds.rb file to create valid comments when you run db:reset;
Initially I tried to run my rails generate commands but kept running into this error:
▶ rake db:migrate
== 20150508143445 CreateComments: migrating ===================================
-- create_table(:comments)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "comments" already exists: CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "comments" already exists: CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
/Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
SQLite3::SQLException: table "comments" already exists
/Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
So I discovered that I needed to delete the old table from the start of the course. I did this in the console.
ActiveRecord::Base.connection.execute("drop table comments")
It seemed to work. I was then able to run these in terminal.
▶ rake db:migrate
== 20150508143445 CreateComments: migrating ===================================
-- create_table(:comments)
-> 0.0015s
== 20150508143445 CreateComments: migrated (0.0016s) ==========================
== 20150508152354 DropComments: migrating =====================================
== 20150508152354 DropComments: migrated (0.0000s) ============================
▶ rails g migration AddCommentToUsers commed_id:integer:index
invoke active_record
create db/migrate/20150508155200_add_comment_to_users.rb
▶ rake db:migrate
== 20150508155200 AddCommentToUsers: migrating ================================
-- add_column(:users, :commed_id, :integer)
-> 0.0010s
-- add_index(:users, :commed_id)
-> 0.0012s
== 20150508155200 AddCommentToUsers: migrated (0.0023s) =======================
models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
end
models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :posts
has_many :comments
# CarrierWave method for attribute functionality
mount_uploader :avatar, AvatarUploader
# These methods check the role of a user in the database
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
end
migrations/create_comments.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :description
t.timestamps null: false
end
end
end
migrations/add_comment_to_users.rb
class AddCommentToUsers < ActiveRecord::Migration
def change
add_column :users, :commed_id, :integer
add_index :users, :commed_id
end
end
In my seeds.rb I have changed
# Create Comments
50.times do
Comment.create!(
# user: users.sample, # we have not yet associated Users with Comments
post: posts.sample,
description: Faker::Lorem.paragraph
)
end
to
# Create Comments
50.times do
Comment.create!(
user: users.sample,
description: Faker::Lorem.paragraph
)
end
Now when I run rake db:reset I get this error.
▶ rake db:reset
-- create_table("advertisements", {:force=>:cascade})
-> 0.0047s
-- create_table("answers", {:force=>:cascade})
-> 0.0010s
-- add_index("answers", ["question_id"], {:name=>"index_answers_on_question_id"})
-> 0.0014s
-- create_table("comments", {:force=>:cascade})
-> 0.0010s
-- create_table("posts", {:force=>:cascade})
-> 0.0010s
-- add_index("posts", ["topic_id"], {:name=>"index_posts_on_topic_id"})
-> 0.0010s
-- add_index("posts", ["user_id"], {:name=>"index_posts_on_user_id"})
-> 0.0014s
-- create_table("questions", {:force=>:cascade})
-> 0.0010s
-- create_table("summaries", {:force=>:cascade})
-> 0.0010s
-- add_index("summaries", ["post_id"], {:name=>"index_summaries_on_post_id"})
-> 0.0010s
-- create_table("topics", {:force=>:cascade})
-> 0.0036s
-- create_table("users", {:force=>:cascade})
-> 0.0015s
-- add_index("users", ["commed_id"], {:name=>"index_users_on_commed_id"})
-> 0.0009s
-- add_index("users", ["comment_id"], {:name=>"index_users_on_comment_id"})
-> 0.0012s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true})
-> 0.0015s
-- add_index("users", ["reset_password_token"], {:name=>"index_users_on_reset_password_token", :unique=>true})
-> 0.0017s
-- initialize_schema_migrations_table()
-> 0.0034s
rake aborted!
ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
/Users/jon/code/Bloccit/db/seeds.rb:45:in `block in <top (required)>'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `times'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `<top (required)>'
-e:1:in `<main>'
Tasks: TOP => db:setup => db:seed
(See full trace by running task with --trace)
Here is the full stack trace:
ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute.rb:138:in `with_value_from_database'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_set.rb:39:in `write_from_user'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:74:in `write_attribute_with_type_cast'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:56:in `write_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:96:in `write_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:373:in `[]='
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:83:in `replace_keys'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:14:in `replace'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/singular_association.rb:17:in `writer'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:123:in `user='
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `public_send'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/core.rb:559:in `init_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/core.rb:281:in `initialize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/persistence.rb:50:in `create!'
/Users/jon/code/Bloccit/db/seeds.rb:45:in `block in <top (required)>'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `times'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `<top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/engine.rb:547:in `load_seed'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:139:in `block (2 levels) in <top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/jon/code/Bloccit/bin/rake:8:in `<top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
-e:1:in `<main>'
Tasks: TOP => db:setup => db:seed
I am stumped here. I thought by deleting the old table and running the new generate commands + migrating everything would be in order. Apparently this is not the case.
What am I doing wrong here?
edited to include line 45 of my seeds.rb
Comment.create!(
You have the wrong migration - instead of adding a user_id to comments you add users.commed.
Oops. It can happen to the best of us.
So first let's create a migration to clean up this mistake:
class RemoveCommedFromUsers < ActiveRecord::Migration
def change
remove_column :users, :commed # will also remove the index
end
end
Of course if the App has not been deployed you could just delete the offending migration and run rake db:reset
So lets create the correct migration
rails g migration AddUserToComments user:belongs_to
Which generates the following migration:
class AddUserToComments < ActiveRecord::Migration
def change
add_reference :comments, :user, index: true
end
end
add_reference creates a index and a foreign key in one sweep.

How to resolve error regarding on MySQL in ROR

Can anybody help me to solve the following error ?When i typed rake db:create on cmd i got these errors.
Error:
rake aborted!
NoMethodError: undefined method `each' for #<String:0x1a1af20>
Tasks: TOP => db:create => db:load_config
(See full trace by running task with --trace)
When i run rake db:create --trace i got the following traces.
rake aborted!
NoMethodError: undefined method `each' for #
Tasks: TOP => db:create => db:load_config
(See full trace by running task with --trace)
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
rake aborted!
NoMethodError: undefined method `each' for #<String:0x1a37888>
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connect
ion_adapters/connection_specification.rb:150:in `resolve_all'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connect
ion_handling.rb:69:in `resolve'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/core.rb
:46:in `configurations='
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/railtie
s/databases.rake:5:in `block (2 levels) in <top (required)>'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:240:in `block i
n execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute
'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:179:in `block i
n invoke_with_call_chain'
C:/Ruby193/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_
with_call_chain'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:201:in `block i
n invoke_prerequisites'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:199:in `each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:199:in `invoke_
prerequisites'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:178:in `block i
n invoke_with_call_chain'
C:/Ruby193/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_
with_call_chain'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:150:in `
invoke_task'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in `
block (2 levels) in top_level'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in `
each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in `
block in top_level'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:115:in `
run_with_threads'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:100:in `
top_level'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:78:in `b
lock in run'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:176:in `
standard_exception_handling'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:75:in `r
un'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.4.2/bin/rake:33:in `<top (required)>
'
C:/Ruby193/bin/rake:23:in `load'
C:/Ruby193/bin/rake:23:in `<main>'
Tasks: TOP => db:create => db:load_config
My database.yml file is described below.
default:&default
adapter:mysql2
encoding:utf8
pool:5
username:root
password:pass
host:localhost
development:<<:*default
database:sqlbook_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:<<:*default
database:sqlbook_test
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:<<:*default
database:sqlbook_production
username:sqlbook
password:pass
I am using rails version 4.2.0 and ruby 1.9.3.Please help me to resolve this error.Thanks in advance..

Alias attriibute for column names in Rails

So, I have a db which was setup for an old php app and I cannot touch the way the DB is done.
However, I have some column name in the db that are using some restricted words in Ruby like "class"
So to make it work, I used alias_attribute.
But it's not working. Here is the code:
class Contractor < ActiveRecord::Base
set_table_name "contractors"
alias_attribute :class_type, :class
attr_accessible :about, :alias, :business_entity, :business_logo, :business_name, :city, :class, :county, :created, :email, :founded, :metroarea, :nid, :phone, :state, :status, :street_address, :uid, :zipcode
end
here are the errors:
1.9.3-p125 :001 > Contractor.last
SyntaxError: /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:345: syntax error, unexpected ')', expecting '='
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:343:in `module_eval'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:343:in `define_optimized_call'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:227:in `block in alias_attribute'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:224:in `each'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:224:in `alias_attribute'
from /Users/snoopy/Projects/buildzoom/ruby/service_request/app/models/contractor.rb:3:in `<class:Contractor>'
from /Users/snoopy/Projects/buildzoom/ruby/service_request/app/models/contractor.rb:1:in `<top (required)>'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `load'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `block in load_file'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:468:in `load_file'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:353:in `require_or_load'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:502:in `load_missing_constant'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `each'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
and
1.9.3-p125 :003 > Contractor.where(:zipcode => "94111")
Contractor Load (648.6ms) SELECT `contractors`.* FROM `contractors` WHERE `contractors`.`zipcode` = '94111'
(Object doesn't support #inspect)
=>
The last one only appear when I remove the alias_attribute otherwise it's the same error as first one
I think your problem is the alias name you gave ":class"
class is one of the ActiveRecord methods
you can see it by running this command
Contractor.first.methods
defining this alias simply "override" it in some way and interrupt to rails operation
So, there is the gem safe_attributes who is doing the work.
It's working perfectly so far.

Rails 3.2 activerecord-jdbc-adapter error

I have just updated to rails 3.2 running on JRuby-1.6.5 and when I run rake db:migrate, I get the following error:
undefined method `accept' for nil:NilClass
Here is the trace:
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in
to_sql'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:36:in
select_values'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/migration.rb:571:in
get_all_versions'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/migration.rb:729:in
migrated'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/migration.rb:652:in
current_version'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/migration.rb:669:in
migrate' org/jruby/RubyEnumerable.java:552:in detect'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/migration.rb:669:in
migrate'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/migration.rb:554:in
up'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/migration.rb:535:in
migrate'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/activerecord-3.2.1/lib/active_record/railties/databases.rake:153:in
(root)' org/jruby/RubyProc.java:258:incall'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/task.rb:205:in
execute' org/jruby/RubyArray.java:1612:ineach'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/task.rb:200:in
execute'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/task.rb:158:in
invoke_with_call_chain'
/Users/paulcowan/.rvm/rubies/jruby-1.6.5/lib/ruby/1.9/monitor.rb:201:in
mon_synchronize'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/task.rb:151:in
invoke_with_call_chain'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/task.rb:144:in
invoke'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/application.rb:116:in
invoke_task'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/application.rb:94:in
top_level' org/jruby/RubyArray.java:1612:ineach'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/application.rb:94:in
top_level'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/application.rb:133:in
standard_exception_handling'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/application.rb:88:in
top_level'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/application.rb:66:in
run'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/application.rb:133:in
standard_exception_handling'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/lib/rake/application.rb:63:in
run'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/gems/rake-0.9.2.2/bin/rake:33:in
(root)' org/jruby/RubyKernel.java:1073:inload'
/Users/paulcowan/.rvm/gems/jruby-1.6.5/bin/rake:19:in `(root)' Tasks:
TOP => db:migrate
The error appears to be coming from activerecord-jdbc-adapter.
I am referencing the master in my gem file:
gem 'activerecord-jdbc-adapter', :git => "https://github.com/nicksieger/activerecord-jdbc-adapter.git", :branch => 'master'
Has anybody else had this error or it there a resolution.