Confusion over trying to get information from Rails Console
I have the following models: Article & Comment.
For Article, it's a has_many :comments
For Comment, it's belongs_to :article
Let's assume they're associated, and functional. They are by the way.
Here's the schema:
ActiveRecord::Schema.define(version: 20160312052519) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.text "body"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["article_id"], name: "index_comments_on_article_id"
end
These are the routes:
resources :articles do
resources :comments
end
When I run commands like:
Article.first.comments
Comment.first.article
Comment.first.article.title
Comment.first.article.text
Comment.first.commenter
Comment.first.body
They all work.
But when I try to run the reverse on Articles like:
Article.first.comments.commenter
Article.first.comments.body
Article.all.comments
They don't work. Why not?
EDIT
This is what I get when I run Article.first.comments.commenter
Article.first.comments.commenter
Article Load (23.0ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT 1
NoMethodError: Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."article_id" = ? [["article_id", 1]]
undefined method `commenter' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x007fa32b7e3d00>
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:136:in `method_missing'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:99:in `method_missing'
from (irb):127
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /Users/aa/dropbox/beginningRails/guideblogagain/bin/rails:9:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/commands/rails.rb:6:in `call'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/command_wrapper.rb:38:in `call'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:185:in `block in serve'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:156:in `fork'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:156:in `serve'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:131:in `block in run'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:125:in `loop'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:125:in `run'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
Article.first.comments is an ActiveRecord_Associations_CollectionProxy object, which is similar to an array. If you want to get commenter for each comment you can use map
Article.first.comments.map(&:commenter)
Or you can get first comment's commenter:
Article.first.comments.first.commenter
Same thing with:
Article.first.comments.body # Won't work
Article.first.comments.map(&:body) # Returns an array (each comment's body)
Article.all.comments # Won't work
Article.all.map(&:comments) # Returns an array (each article's comments)
When you run your working example Article.first.comments, take a look at the actual return value. What do you see? The return is an array of objects, right? So say that Article.first has 10 Comments. When you run your query, ActiveRecord will return you an array of 10 Comment objects.
Now let's take a look at one of your non-working examples.
Say, Article.first.comments.commenter. Again we start with Article.first, which we know returns us the first Article object in our DB.
But the next step is to think about Article.first.comments.
We've already convinced ourselves that that's an array of Comment objects, right? And herein lies the problem -- commenter is an attribute on a single Comment object.
But you don't have a single Comment object -- you have an Array of Comment objects and commenter isn't defined on an Array.
The Article.first.comments.body issue is the same. Does that make sense?
One thing you could do, in your console, is the following:
Article.first.comments.each do |comment|
puts comment.commenter
end
And then you can get at all of your commenter/body values.
Using this line of thinking, I think you'll be able to convince yourself of the issue with Article.all.comments, but if not, feel free to comment and we can talk it out.
Here are the docs for Active Record relations, for reference. Cheers!
Related
I've been getting this persistent error every time I try to update my table in rails.
For example if my code in the schema.rb file is:
create_table "books", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
If I go into the rails console and write the command:
book = Book.new(title: "a book", description: "a book description")
it gives me the following error:
Errno::EBADF (Bad file descriptor)
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/input-method.rb:42:in `winsize'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/input-method.rb:42:in `winsize'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:753:in `output_value'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:545:in `block (2 levels) in eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:704:in `signal_status'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:538:in `block in eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:166:in `block (2 levels) in each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:151:in `loop'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:151:in `block in each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:150:in `catch'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:150:in `each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:537:in `eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:472:in `block in run'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:471:in `catch'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:471:in `run'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:400:in `start'
Maybe IRB bug!
I can continue and write book.save which will update the new book into the database however I not sure if this error message is a concern. It wasn't there previously when running this command and is now there on all projects when trying to update any tables.
I've completely reinstalled Ruby 2.7 and RubyMine and this error still returns. I'm not sure what this problem is caused by.
Thank you for any advice.
(Info: working on Windows machine on RubyMine. Using rails 5.2.4 and Ruby 2.7.1)
That appears to be a known problem with IRB and Ruby 2.7 so could you please add your vote there:
https://youtrack.jetbrains.com/issue/RUBY-26249
In a new Rails 6 project, I have a table named object_classes with a column named ClassList_id. From schema.rb:
create_table "object_classes", force: :cascade do |t|
t.string "name"
t.integer "ClassList_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["ClassList_id"], name: "index_object_classes_on_ClassList_id"
end
I've realized that the column should be named class_list_id to conform with Rails expected naming convention. Therefore, I have generated a new migration:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
rename_column :object_classes, :ClassList_id, :class_list_id
end
end
However, when I run this migration, I get the following error:
/bin/bash -c "env RBENV_VERSION=2.6.1 /home/asfarley/.rbenv/libexec/rbenv exec bundle exec ruby /home/asfarley/imgseq/bin/spring rails 'db:migrate'"
== 20200716060501 FixColumnName: migrating ====================================
-- rename_column(:object_classes, :ClassList_id, :class_list_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Process finished with exit code 1
What am I doing wrong here? I'm looking for an explanation that addresses specifically what is wrong here, so that I can understand how to avoid this in the future.
Hard to know whether the fault is Rails or SQLite, but the issue seems to be that I had a foreign key defined for my object_classes table pointing to a table that didn't exist. With this migration, I was able to fix the foreign key constraint and rename the column:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
remove_foreign_key :object_classes, :ClassLists
rename_column :object_classes, :ClassList_id, :class_list_id
add_foreign_key :object_classes, :class_lists
end
end
your syntax is ok so the error must be a reserved name error. may try and remove the column and creating the column again, watch out for capital letters on migrations for they can unnecessarily complicate your code. try this:
rails g migration RemoveClassListIdFromObjectClasses
that will created a migration kinda like this:
def drop
remove column :object_classes, :class_lis_id
end
and then run
rails g migration AddClassListIdToObjectClasses
the migration should look something like this:
def change
add_column :object_classes, :class_list_id
end
remember to add the data type and anything else you may need.
there you can check the migration it created and you can change it to maybe add the data type or maybe change the capitalization of the column and you will be set to go.
note: if it works i would appreciate if you accepted the answer, Thanks!
Sorry for my English.
I searched the site, but I could not find a solution.
When I try to make rails db: migrate gives me this error:
C:\PW\Sites\grota>rails db:migrate
== 20170209014957 CreateWholesalers: migrating ================================
-- create_table(:wholesalers)
-- limit()
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
undefined local variable or method `limit' for # <CreateWholesalers:0x00000006bfc6a0>
C:/PW/Sites/grota/db/migrate/20170209014957_create_wholesalers.rb:7:in `block in change'
C:/PW/Sites/grota/db/migrate/20170209014957_create_wholesalers.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
NameError: undefined local variable or method `limit' for # <CreateWholesalers:0x00000006bfc6a0>
C:/PW/Sites/grota/db/migrate/20170209014957_create_wholesalers.rb:7:in `block in change'
C:/PW/Sites/grota/db/migrate/20170209014957_create_wholesalers.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
here is my migrate code:
class CreateWholesalers < ActiveRecord::Migration[5.0]
def change
create_table :wholesalers do |t|
t.string "brand_name", :limit => 25
t.string "username", :limit => 25
t.string "password_digest"
t.string "email", limit => 50
t.decimal "latitude"
t.decimal "longitude"
t.timestamps
end
end
end
Thank you in advance.
This line is causing the error (missing :):
t.string "email", :limit => 50
Cristiano Morais should be marked as correct answer. It is a spelling mistake. Missing :
So I'm following the http://www.railstutorial.org/book, and evrything works fine locally (running sqlight3).
I get the following error when I try
heroku run rake db:migrate
This is what the error message looks like
Running rake db:migrate attached to terminal... up, run.4049
Migrating to AddPasswordDigestToUsers (20140817014655)
== 20140817014655 AddPasswordDigestToUsers: migrating =========================
-- add_column(:users, :password_digest, :string) PG::Error: ERROR: column "password_digest" of relation "users" already exists : ALTER
TABLE "users" ADD COLUMN "password_digest" character varying(255) rake
aborted! StandardError: An error has occurred, this and all later
migrations canceled:
PG::Error: ERROR: column "password_digest" of relation "users"
already exists : ALTER TABLE "users" ADD COLUMN "password_digest"
character
varying(255)/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in
exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in
block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:442:in block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.8/lib/active_support/notifications/instrumenter.rb:20:in
instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:437:in log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in
execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/schema_statements.rb:360:in
add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/schema_statements.rb:395:in
add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:629:in
block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:601:in
block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:601:in
say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:621:in
method_missing'
/app/db/migrate/20140817014655_add_password_digest_to_users.rb:3:in
change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:575:in
exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:559:in
block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:558:in
block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in
with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:557:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:713:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:963:in
block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:1009:in
block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in
block in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in
within_new_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in
transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:209:in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:1009:in
ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:962:in
execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:924:in
block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:920:in
each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:920:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:768:in
up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:746:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/railties/databases.rake:42:in
`block (2 levels) in ' Tasks: TOP => db:migrate (See
full trace by running task with --trace)
I already tried heroku pg:reset DATABASE_URL and then retrying.
Also tried adding /spec, /lib, /script, /features, /cucumber.yml to .slugignore
This is what my schema looks like:
ActiveRecord::Schema.define(version: 20140818041701) do
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "remember_token"
t.boolean "admin", default: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["remember_token"], name: "index_users_on_remember_token"
end
And this is what my password digest migration looks like
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
add_column :users, :password_digest, :string
end
end
You already have password_digest column in your table and with migration AddPasswordDigestToUsers you are trying to create another one that's why it creating issue. Remove migration AddPasswordDigestToUsers and try run rake db:migrate again. Sqlite3 not producing any error in such cases. if you write intege instead of integer in migration it will not produce any error if you use sqlite.
If you simply delete the password digest migration file, it will solve the short term problem of deploying to heroku but then it won't be available for collaborators to pull down and set up their database locally. They will run into issues and may create another migration file which creates the same problem.
I'm not sure the best solution, but this seems to be working out for me so far:
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
if Rails.env == "production"
else
add_column :users, :password_digest, :string
end
end
end
I'm new to Rails and I'm building a simple project-tracking app for my employer. I've been developing the app on my Mac and pushing it to github. I just managed to clone my github repo to a windows box behind my company's firewall in hopes of letting colleagues try the app out.
But when I go to rake db:migrate to initialize the database on the windows box, I get the following error messages:
$ rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
Could not find table 'projects'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/connection_adapters/sqlite3_adapter.rb:29:in `table_structure'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/faker-0.3.1/lib/extensions/object.
rb:3:in `returning'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/connection_adapters/sqlite3_adapter.rb:28:in `table_structure'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/connection_adapters/sqlite_adapter.rb:228:in `columns'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:1271:in `columns'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:1279:in `columns_hash'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:1578:in `find_one'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:1569:in `find_from_ids'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_reco
rd/base.rb:616:in `find'
c:/Rails_Projects/molex_app/config/routes.rb:15
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:226:in `draw'
c:/Rails_Projects/molex_app/config/routes.rb:1
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:145:in `load_without_new_constant_marking'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:145:in `load'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:521:in `new_constants_in'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:145:in `load'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:286:in `load_routes!'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:286:in `each'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:286:in `load_routes!'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_contro
ller/routing/route_set.rb:266:in `reload!'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:537
:in `initialize_routing'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:188
:in `process'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:113
:in `send'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:113
:in `run'
c:/Rails_Projects/molex_app/config/environment.rb:9
c:/RubyonRails/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `
gem_original_require'
c:/RubyonRails/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `
require'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:156:in `require'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:521:in `new_constants_in'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_sup
port/dependencies.rb:156:in `require'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/tasks/misc.rake:4
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `cal
l'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `exe
cute'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:612:in `eac
h'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:612:in `exe
cute'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:578:in `inv
oke_with_call_chain'
c:/RubyonRails/Ruby187/lib/ruby/1.8/monitor.rb:242:in `synchronize'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:571:in `inv
oke_with_call_chain'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:588:in `inv
oke_prerequisites'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:585:in `eac
h'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:585:in `inv
oke_prerequisites'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:577:in `inv
oke_with_call_chain'
c:/RubyonRails/Ruby187/lib/ruby/1.8/monitor.rb:242:in `synchronize'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:571:in `inv
oke_with_call_chain'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:564:in `inv
oke'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2019:in `in
voke_task'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `to
p_level'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `ea
ch'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `to
p_level'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in `st
andard_exception_handling'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1991:in `to
p_level'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1970:in `ru
n'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in `st
andard_exception_handling'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1967:in `ru
n'
c:/RubyonRails/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.3/bin/rake:31
c:/RubyonRails/Ruby187/bin/rake:19:in `load'
c:/RubyonRails/Ruby187/bin/rake:19
My gems list looks like this:
$ gem list
*** LOCAL GEMS ***
actionmailer (2.3.5)
actionpack (2.3.5)
activerecord (2.3.5)
activeresource (2.3.5)
activesupport (2.3.5)
faker (0.3.1)
rack (1.0.1, 1.0.0)
rails (2.3.5)
rake (0.8.3)
sqlite3-ruby (1.3.0 x86-mingw32)
will_paginate (2.3.12)
My schema.rb file on the Windows box looks like this:
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20100915193510) do
create_table "assets", :force => true do |t|
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
create_table "macroposts", :force => true do |t|
t.text "content"
t.integer "user_id"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects", :force => true do |t|
t.string "title"
t.integer "status", :limit => 255
t.integer "program_manager_id"
t.integer "design_engineer_id"
t.string "sales_engineer"
t.string "customer"
t.string "market_size"
t.string "project_code"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "projects", ["design_engineer_id"], :name => "index_projects_on_design_engineer_id"
add_index "projects", ["program_manager_id"], :name => "index_projects_on_program_manager_id"
create_table "statuses", :force => true do |t|
t.string "status_name"
t.integer "status_code"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "encrypted_password"
t.string "salt"
t.string "remember_token"
t.boolean "admin", :default => false
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
end
Since this is the first time I've run migrations on this machine, I would expect that nothing about the migration process should seek to access the 'projects' table before it's created. But I notice that the "projects" table isn't listed in schema.rb until after several tables that are associated with it (assets, macroposts.) Is that the source of the problem?
Or is some sort of gem dependency issue at work here? I noticed that the 'faker' gem shows up towards the top of the error listing, even though I don't even really make use of it except in my tests (copied from Michael Hartl's RailsTutorial.org.)
Thanks for any help or suggestions you can offer!
Dean Richardson
Genlighten.com
I had this exact problem for the last few weeks on a side project I've been working on. The answer only became clear to me when I ran rake db:migrate with --trace. Once there I noticed that the stack trace was complaining about factory_girl not being able to instantiate the missing table item.
I went into my factory.rb file and removed the offending model and after that I was able to run rake db:migrate. In addition, the problem for me was:
Factory.define :table1 do |b|
b.name "table1"
b.rating 5
b.table2_relation Factory(:table2)
end
In this case the Factory inside the factory was attempting to insert during the rake task. Since the table didn't exist yet it blew up. With a little help I found that the correct way to do this is:
Factory.define :table1 do |b|
b.name "table1"
b.rating 5
b.table2_relation {Factory(:table2)}
end
I just encountered this same problem. rake db:create would work, but running anything else, like rake db:schema:load or rake db:migrate, failed with an error about a missing table. Chris Hein's answer pointed me in the right direction. The missing table was being referenced in the specs, and for some reason they were being loaded as part of the environment before the rake tasks were running. I just moved the specs folder, ran the rake tasks, and the moved it back.
might want to try to rake db:create also make sure your config files are correct in pointing to the sql server you want.
Also make sure all of your migration files look good and don't have any syntax errors or anything like that.
Had similar issue and it was to do with factory_girl as Chris pointed out. This way seems like an easier way to solve it:
In Gemfile, define factory girl like this:
gem 'factory_girl_rails', :require => false
Then in spec_helper.rb file:
require 'factory_girl_rails'
Here is the link to original answer: FactoryGirl screws up rake db:migrate process