The RSpec test suite for my project https://github.com/phoet/on_ruby fails when updating Rails from 3.2.6 to 3.2.7.
rake spec
1) Authorization should create an auth and a user from an auth-hash
Failure/Error: Authorization.create_from_hash(auth)
ActiveRecord::RecordNotUnique:
SQLite3::ConstraintException: column nickname is not unique: INSERT INTO "users" ("admin", "available", "created_at", "description", "freelancer", "github", "hide_jobs", "image", "location", "name", "nickname", "slug", "updated_at", "url") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
# ./app/models/user.rb:74:in `create_from_hash!'
# ./app/models/authorization.rb:15:in `create_from_hash'
# ./spec/models/authorization_spec.rb:23:in `block (3 levels) in <top (required)>'
# ./spec/models/authorization_spec.rb:22:in `block (2 levels) in <top (required)>'
Google Geocoding API error: over query limit. | ETA: 00:00:03
2) Location finder should find users within the default scope
Failure/Error: Location.unscoped.all.size.should be(2)
expected #<Fixnum:5> => 2
got #<Fixnum:21> => 10
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
'actual.should eq(expected)' if you don't care about
object identity in this example.
# ./spec/models/location_spec.rb:16:in `block (3 levels) in <top (required)>'
This problem does not occur when running RSpec directly, nor does it occur on travis: http://travis-ci.org/#!/phoet/on_ruby/builds/1989858
It looks as if the transactions do not rollback properly. Did someone experience a similar behavior?
Any Ideas?
I'm getting the same. From what I can tell, rails is now sets ENV['RAILS_ENV']=development, so your tests are running against you development database. Try rake spec RAILS_ENV=test
It looks like it's this issue: https://github.com/rails/rails/issues/7175
Related
For my rails application, I have a "Posts" table that has the columns: id, description, user_id, created_at, updated_at.
Locally, I ran the following query:
ALTER TABLE posts RENAME TO posts_old;
Then in my rails console, I ran:
require Rails.root + 'db/migrate/20130425060156_create_posts.rb'
CreatePosts.new.change
Then I ran the query:
INSERT INTO posts (id, user_id, content, created_at, updated_at)
SELECT id, user_id, content, created_at, updated_at FROM posts_old;
The query ran fine, but the problem I realized is that I was missing a "user_id" column in my new "Posts" table.
So I ran a migration and created a "user_id" column for the new "Posts" table.
I pushed up these changes up to git.
Lastly, I pushed these changes up to heroku. Due to the last migration: I ran
heroku run rake db:migrate
This is when I got an error. See below. My question is, how do I add a column to my new "Posts" table without conflicting with what is existing in my database on heroku?
Migrating to AddUserIdToPost (20131008050154)
== AddUserIdToPost: migrating ================================================
-- add_column(:posts, :user_id, :integer)
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "user_id" of relation "posts" already exists
: ALTER TABLE "posts" ADD COLUMN "user_id" integer/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:649:in `execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1022:in `add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:466:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:458:in `method_missing'
/app/db/migrate/20131008050154_add_user_id_to_post.rb:3:in `change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:407:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:389:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:528:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `call'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:719:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:570:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:551:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
I suspect that the migration AddUserIdToPost is performed after the migration CreatePosts. And migration CreatePosts includes the column user_id. Remove "user_id" from migration CreatePosts. The attribute "user_id" will be created in migration AddUserIdToPost.
Updated AddUserIdToPost migration from
def change
add_column :posts, :user_id, :integer
end
to this:
def up
add_column :posts, :user_id, :integer
end
def down
end
Then ran the following:
bundle exec rake db:rollback STEP=1
bundle exec rake db:migrate
Then push up to git, heroku, and run heroku migration.
All of a sudden I started to get two different errors with one of my Models. I have no idea what I changed, but my code doesn't seem to be the problem.
I entered the rails console. I typed QuestiNon.find(:all) and I got all the records I have entered so far. But, if I try
ruby-1.8.7-p352 :003 > Question.find(55)
NoMethodError: undefined method `eq' for nil:NilClass
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:299:in `find_one'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:289:in `find_with_ids'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:107:in `find'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/base.rb:444:in `__send__'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/base.rb:444:in `find'
from (irb):3
I have double checked and the id 55 does exist
And if I try the create method I get:
ruby-1.8.7-p352 :004 > Question.create("question" => "something")
NoMethodError: undefined method `name' for nil:NilClass
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:56:in `visit_Arel_Nodes_InsertStatement'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:55:in `map'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:55:in `visit_Arel_Nodes_InsertStatement'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `send'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `visit'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:5:in `accept'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:18:in `accept'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:111:in `with_connection'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:16:in `accept'
Googling around some people suggested the problem is due to a primary_key configuration, but I have not set a new primary key. My models primary key should be id.
My question.rb is like this:
class Question < ActiveRecord::Base
set_table_name "questions"
end
and my table is like this:
from (irb):4ruby-1.8.7-p352 :005 > ActiveRecord::Base.connection.columns("questions").map{|c| [c.name, c.type] }
=> [["id", :integer], ["question_type", :integer], ["question", :string], ["mandatory", :boolean], ["help", :string], ["answers", :string], ["question_number", :integer], ["survey_id", :string], ["created_at", :datetime], ["updated_at", :datetime]]
The weird thing is that this is working fine in my server with MYSQL, but locally with SQLITE3 it is not.
How can I fix this? Any ideas? Thanks.
I have a Deal model and one Category model having many to many mapping (has_many:through, as i m saving some related data too.)
Also I have a city model which is also having many to many mapping with deal model (Again has_many through)
Category model has no relationship with City model.
Now to search deals based upon keywords (Keywords means, i have indexed some columns like title of the deal, description etc)
#deals = Deal.search(params[:keywords],:per_page => 20, :page => params[:page])
Question is:
Now I need to group these deals based upon the categories, basically first category has 4 deals, second category has 7 deals and so on provided city_id is given.
#deals = #city.deals.search(params[:keywords],:per_page => 20, :page => params[:page])
PS:
You can think of something like this:
End user will enter Select London as a location and then enter "fun" as a keyword and then i need to search the database to check the london deals having having "fun" keyword and group them based upon the various categories.
I hope i am able to explain the question.
EDIT
Some issues with indexing:
rake thinking_sphinx:rebuild --trace
** Invoke thinking_sphinx:rebuild (first_time)
** Invoke thinking_sphinx:app_env (first_time)
** Execute thinking_sphinx:app_env
** Execute thinking_sphinx:rebuild
** Invoke thinking_sphinx:index (first_time)
** Invoke thinking_sphinx:app_env
** Execute thinking_sphinx:index
Generating Configuration to /Users/me/projects/deals/config/development.sphinx.conf
rake aborted!
Cannot automatically map attribute category_ids in Deal to an
equivalent Sphinx type (integer, float, boolean, datetime, string as ordinal).
You could try to explicitly convert the column's value in your define_index
block:
has "CAST(column AS INT)", :type => :integer, :as => :column
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/attribute.rb:334:in `translated_type_from_database'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/attribute.rb:170:in `type'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/attribute.rb:139:in `include_as_association?'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/attribute.rb:107:in `to_select_sql'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source/sql.rb:69:in `block in sql_select_clause'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source/sql.rb:69:in `collect'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source/sql.rb:69:in `sql_select_clause'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source/sql.rb:19:in `to_sql'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source.rb:117:in `set_source_sql'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source.rb:51:in `to_riddle_for_core'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:114:in `block in to_riddle_for_core'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:113:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:113:in `each_with_index'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:113:in `to_riddle_for_core'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:83:in `to_riddle'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/active_record.rb:245:in `block in to_riddle'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/active_record.rb:244:in `collect'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/active_record.rb:244:in `to_riddle'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/configuration.rb:164:in `block in generate'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/configuration.rb:161:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/configuration.rb:161:in `generate'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/configuration.rb:177:in `build'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/tasks.rb:78:in `block (2 levels) in <top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/tasks.rb:95:in `block (2 levels) in <top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_task'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:90:in `block (2 levels) in top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:90:in `block in top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:62:in `block in run'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/Users/me/.rvm/gems/ruby-1.9.2-p290#global/gems/rake-0.9.2/bin/rake:32:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/Users/me/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
Tasks: TOP => thinking_sphinx:index
I supposed that you have a Deal model like
class Deal < ActiveRecord::Base
has_many :category_deals
has_many :categories, :through => :category_deals
has_many :city_deals
has_many :cities, :through => :city_deals
define_index do
indexes :name
has category_deals.category_id, :as => :category_ids
has city_deals.city_id, :as => :city_ids
end
end
So you want to search by city and given category or categories (1,3,2) for the given city with categories ASC order:
Deal.search(params[:keywords], :with => {:category_ids => [1,3,2], :city_ids => params[:city_id]}, :order => "category_ids ASC", :per_page => 20, :page => params[:page])
I'm not entirely sure what you're after - but let's clarify a few things, particularly which results should be returned, and then the order of results.
Firstly, to limit search results to a given city, you'll need the city ids as an attribute in your Deal index definition (speaking of which - it'll help if you can provide the existing define_index block in Deal):
has cities.id, :as => :city_ids
Then, for sorting, it sounds like you want to have search results grouped together by category - but this is tricky, because a deal may belong to more than one category. You can't sort by a MVA attribute (e.g.: the collection of category ids for a given deal). So what exactly do you want to get out of the search results?
UPDATE 4
See solution below!
**Update 3 **
If anyone is actually still reading this (I appreciate it!) I've been struggling around my code today (yes still a noob and in chapter 11 of the ruby on rails tutorial).
I've supposedly reset back to my last commit:
$ cd rails_screencast/sample_app/
$ git reset --hard 2396c0d288d132ffc43c82d5cbbc736a5258eed2
HEAD is now at 2396c0d Micropost Validations
When I check the site on local host it actually shows a list of users and not an ERROR page but when I run the test suite (autotest with spork - I've reset those a few times too to be sure) I still get all errors such as these shown below. I'm very curious about the "Could not find table 'users'" as it is in EVERY error #
108) Users signin success should sign a user in and out
Failure/Error: user = Factory(:user)
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/requests/users_spec.rb:56:in `block (4 levels) in <top (required)>'
Finished in 0.9872 seconds
108 examples, 108 failures
Time to keep on plugging and looking at all these errors, if anyone can offer any insight or hints or ideas what to check I would appreciate it! Or if you need additional information I can post that too (just be very specific to which files to see as I'm not totally fluent in the lingo as many are already)
Update 2:
It seems that my reset messed something up with the factory settings as sometimes all my errors are pointing to line 5 of my micropost_spec.rb file specifically # the #user = Factory(:user) line... almost like my factories file isn't linked to anything any more. I am wondering if a rake db:migrate would solve any of my problems... or just create new problems... I only have sample data filling in with faker
Any ideas?
Is it possible to revert my file system back to a previous commit and start over? ... when I was green
Failures:
1) Micropost should create a new instance with valid attributes
Failure/Error: #user = Factory(:user)
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'
2) Micropost user associations should have a user attribute
Failure/Error: #user = Factory(:user)
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'
3) Micropost user associations should have the right associated user
Failure/Error: #user = Factory(:user)
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'
4) Micropost validations should have a user id
Failure/Error: #user = Factory(:user)
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'
5) Micropost validations should require nonblank content
Failure/Error: #user = Factory(:user)
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'
6) Micropost validations should reject long content
Failure/Error: #user = Factory(:user)
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'
Update 1:
I'm getting errors from 4 to 6 to 111 all with a recurring theme of
1) Micropost should create a new instance with valid attributes
Failure/Error: #user = Factory(:user)
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/micropost_spec.rb:53:in `block (2 levels) in <top (required)>'
Specifically the "Could not find table 'users'" - I copied the code from the tutorial git for the micropost_spec.rb file but didn't seem to fix it- maybe someone can help direct me to what the table is referring to?
////////////////////////////////////// ORIGINAL BELOW ////////////////////////////////
I am working in the Ruby on Rails Tutorial lesson 11 and am trying to populate my db to show the microposts
when I did the rake db:populate command it gave me the following:
macbook:sample_app macbook$ rake db:populate
(in /Users/macbook/rails_screencast/sample_app)
db/test.sqlite3 already exists
db/test.sqlite3 already exists
db/development.sqlite3 already exists
-- create_table("microposts", {:force=>true})
-> 0.0090s
-- add_index("microposts", ["user_id"], {:name=>"index_microposts_on_user_id"})
-> 0.0074s
-- create_table("users", {:force=>true})
-> 0.0243s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true})
-> 0.0094s
-- initialize_schema_migrations_table()
-> 0.0167s
-- assume_migrated_upto_version(20110614132314, "db/migrate")
-> 0.0432s
rake aborted!
Validation failed: Email is invalid
I ran it a couple times and kept getting the same error... when I visited the site on a browser it was like none of my sample users were there...
require 'faker'
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
admin = User.create!(:name => "Foo Bar",
:email => "foo#bar.com",
:password => "foobar",
:password_confirmation => "foobar")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
User.all(:limit => 6).each do |user|
50.times do
user.microposts.create!(:content => Faker::Lorem.sentence(5))
end
end
end
end
then I decided oh maybe rake db:reset will purge my sample database and I can run the populate again with fresh stuff... bad assumption
my test suite now shows:
Finished in 0.99915 seconds
111 examples, 111 failures
Now before I mess more stuff up I'm looking for some advice where to go next ...
Michael (author of the railstutorial.org) responded to my email request!
If you're missing a table when running the test suite, it's probably an indication that you need to run:
rake db:test:prepare
YUP!! YAYYYY
Finished in 4.82 seconds
108 examples, 0 failures
CAKE TIME!
The db:populate that you're showing does a db:reset itself, so doing it manually can't have caused any problem that wasn't already happening with the db:populate.
You can just delete your db/*.sqlite3 files and start from fresh. There's nothing obvious in that db:populate code which would explain why the email validation failed, you'd have to show the User model and the validations you have in there for us to diagnose that initial issue.
I'm just learning MongoDB and MongoMapper. This is on Rails 3.
I created a blog in app/models/blog.rb:
class Blog
include MongoMapper::Document
key :title, String, :required => true
key :body, Text
timestamps!
end
I go into the Rails console:
rails c
Loading development environment (Rails 3.0.0.beta)
ruby-1.9.1-p378 > b = Blog.new
NoMethodError: undefined method `from_mongo' for Text:Module
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:323:in `get'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:269:in `read_key'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:224:in `[]'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:7:in `block in inspect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:6:in `collect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:6:in `inspect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:47:in `start'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:8:in `start'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands.rb:34:in `<top (required)>'
from /Users/jade/code/farmerjade/script/rails:10:in `require'
from /Users/jade/code/farmerjade/script/rails:10:in `<main>'
Am I overlooking something really dumb, or is this something in my setup?
I'm using the mongo_mapper version you get by adding it to your Gemfile, so I'm wondering if it might be that. I'd appreciate any suggestions!
As I suspected, this was a silly mistake. I was using Text instead of String, and Mongo doesn't natively support Text as a key. If you define a from_mongo method, it would.
Thanks to DanP on IRC for pointing out my error.