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

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.

Related

Getting PendingMigrationError while running the Rails server on Ubuntu

I am getting the following error while running rails server on Ubuntu machine.
Error:
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
For resolving this issue i again run rake db:migrate but got the below error.
Error2:
== 20141218112307 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
undefined method `inet' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000008c00370>/home/subhrajyoti/100-Sallon/db/migrate/20141218112307_devise_create_users.rb:19:in `block in change'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:200:in `create_table'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:462:in `create_table'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:662:in `block in method_missing'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `block in say_with_time'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `say_with_time'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:652:in `method_missing'
/home/subhrajyoti/100-Sallon/db/migrate/20141218112307_devise_create_users.rb:3:in `change'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:606:in `exec_migration'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:589:in `block in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:588:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:765:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:1043:in `ddl_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in `block in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `each'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
NoMethodError: undefined method `inet' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000008c00370>
/home/subhrajyoti/100-Sallon/db/migrate/20141218112307_devise_create_users.rb:19:in `block in change'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:200:in `create_table'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:462:in `create_table'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:662:in `block in method_missing'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `block in say_with_time'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `say_with_time'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:652:in `method_missing'
/home/subhrajyoti/100-Sallon/db/migrate/20141218112307_devise_create_users.rb:3:in `change'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:606:in `exec_migration'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:589:in `block in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:588:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:765:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:1043:in `ddl_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in `block in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `each'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
20150121104519_add_facilities_to_businesses.rb:
class AddFacilitiesToBusinesses < ActiveRecord::Migration
def change
add_column :businesses, :facilities, :string, array: true, default: []
add_index :businesses, :facilities, using: 'gin'
end
end
Actually i already have a Rails app which was with other database before and now i am trying it to connect mysql and getting these errors.I am using Rails version 4.2.0 and ruby 2.1.7.Please help me to resolve this errors.
Error explain everything itself. You have problem in your database migration file. Go to application_folder/db/migrate and find file which contain name devise_create_users.rb.
Inside that file mistakenly you wrote t.inte which should be t.integer.
So try change t.inte to t.integer and you are done. If you get any other error in migration then follow this.
Remove gin from migration and run rake db:migrate again. gin specific for postgres indexing.
class AddFacilitiesToBusinesses < ActiveRecord::Migration
def change
add_column :businesses, :facilities, :string, array: true, default: []
add_index :businesses, :facilities
end
end

"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.

one to many relationship ruby on rails

So I made changes to my changes to subject.rb file here
class CreateSubjects < ActiveRecord::Migration
def up
create_table :subjects do |t|
t.string "name"
t.integer "position"
t.boolean "visible", :default =>false
t.timestamps
end
end
def down
drop_table :subjects
end
end
and then I went ahead and defined page.rb file in
class Page < ActiveRecord::Base
belongs_to :subject
end
Then I got to the terminal
Muhammeds-MacBook-Pro:simple_cms muhammedz786$ rails c
Loading development environment (Rails 4.0.2)
irb(main):001:0> subject = Subject.find(1)
Subject Load (0.3ms) SELECT subjects.* FROM subjects WHERE subjects.id = 1 LIMIT 1
=> #
Then I tried to call the subject.page method and I got the follwoing mistake! What does this mean?
irb(main):002:0> subject.page
**Mysql2::Error: Table 'simple_cms_development.pages' doesn't exist: SHOW FULL FIELDS FROM `pages`**
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'simple_cms_development.pages' doesn't exist: SHOW FULL FIELDS FROM `pages`
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `query'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `block in execute'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.0.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `execute'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/mysql2_adapter.rb:222:in `execute'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:301:in `execute_and_free'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:462:in `columns'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:118:in `block in prepare_default_proc'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:67:in `yield'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:67:in `columns_hash'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:25:in `column_for'
... 3 levels...
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:44:in `each_with_index'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:44:in `add_constraints'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:19:in `scope'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:103:in `association_scope'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:87:in `scope'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/singular_association.rb:42:in `find_target'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:147:in `load_target'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:54:in `reload'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/singular_association.rb:9:in `reader'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/builder/association.rb:70:in `page'
from (irb):2
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
Make sure that your Pages model has a subject_id in the migration, and then make sure to run rake db:migrate

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.

Ruby on Rails Import CSV into 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.