I have a study-case project, working with Rails 4, RSpec, FactoryGirl, Faker...
But rspec tests are failing randomly, on development env. I'am searching a lot, but if anyone could help me figure it out, I would appreciated.
Here are the links to the test file, factory and the model:
Model to be tested: https://github.com/freaktags/core/blob/master/app/models/user.rb
User Factory: https://github.com/freaktags/core/blob/master/spec/factories/users.rb
Test itself: https://github.com/freaktags/core/blob/master/spec/models/user_spec.rb
When it fails, produce always a stack almost like this:
Failure/Error: #chloe = FactoryGirl.create(:user)
ActiveRecord::RecordInvalid:
translation missing: pt-BR.activerecord.errors.messages.record_invalid
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/validations.rb:57:in `save!'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:273:in `block in save!'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:208:in `transaction'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:273:in `save!'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `[]'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `create'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353#global/gems/activesupport-4.1.8/lib/active_support/notifications.rb:161:in `instrument'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
# /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'
You changed the locale to something other than the rails default and are getting errors that don't have a translation into that locale.
An easy work-around would be to temporarily change the locale back to the default so you are more likely to get the actual error message (Looks like activerecord is trying to tell you something but it doesn't know how to say it in pt-BR). It will tell you this error in English, but it seems like you know English so that shouldn't be a problem.
Fix the error, then switch the locale back to your desired locale and the error shouldn't appear any more.
If you need to avoid changing the locale you will need to add a translation appropriate to your chosen locale for the specific error. There are sources elsewhere that can help with this process. Here are some links with related information:
http://guides.rubyonrails.org/i18n.html
http://archive.railsforum.com/viewtopic.php?id=46725
ActiveRecord error messages: translation for fields
Update:
A shot in the dark, but your specific error may have to do specifically with FactoryGirl having issues during initialization. . . This answer looks like it may work with your problem: https://stackoverflow.com/a/11644746/1026898.
The problems was Faker, it doesn't provide a control for email and username values. Anyway Ecnalyr helped a lot on finding the problem :)
Related
This seemed to be a flickering sort of a bug for a while,but now it's appearing consistently: when I run RSpec on a fairly simple ApplicationHelper spec, I get the following error:
% rspec --backtrace
1) ApplicationHelper renders Markdown from plain text
Failure/Error: expect(helper.md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
NameError:
undefined local variable or method `helper' for #<RSpec::ExampleGroups::ApplicationHelper_2:0x000001248d1218>
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-expectations-0f7b78587ab4/lib/rspec/matchers.rb:903:in `method_missing'
# ./spec/helpers/application_helper_spec.rb:4:in `block (2 levels) in <top (required)>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:148:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:148:in `block in run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `call'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `block (2 levels) in <class:Procsy>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-rails-480b173c9ad6/lib/rspec/rails/adapters.rb:67:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:292:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:292:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:430:in `block (2 levels) in run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `call'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `block (2 levels) in <class:Procsy>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:432:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:485:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:301:in `with_around_example_hooks'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:145:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:494:in `block in run_examples'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:490:in `map'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:490:in `run_examples'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:457:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `map'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/reporter.rb:49:in `report'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:86:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:70:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:38:in `invoke'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/exe/rspec:4:in `<top (required)>'
# /Users/danielsh/Dropbox/Project/Websites/Angular/bin/rspec:20:in `load'
# /Users/danielsh/Dropbox/Project/Websites/Angular/bin/rspec:20:in `<main>'
Here's the complete spec file (spec_helper is included as part of my .rspec file):
describe ApplicationHelper do
it 'renders Markdown from plain text' do
plaintext = '# Header'
expect(helper.md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
end
end
This was working up until recently, but I'm not certain what I could have done to break such a basic feature. I'm using edge versions of Rails and RSpec, but didn't see anything in their git repos to suggest that helper had been deprecated---and running rails g helper foo still generates a foo_helper_spec.rb file with instructions indicating that helper contains the helper itself. If anyone has any ideas, I'd be grateful for them!
I created a new Rails project with a fresh RSpec installation, and it led me to the problem. Apparently one of the recent betas introduced a configuration directive called config.infer_spec_type_from_file_location! that was missing from my slightly older spec_helper file; without it, RSpec wasn't guessing the spec type and mixing in the associated methods. Beware breaking changes!
add :type => :helper
so your code should look like
describe ApplicationHelper, type: :helper do
...
end
That's a weird error !! are you sure you required spec_helper in your spec ?
Anyway you could try without the helper method:
First you should first add to /spec/spec_helper.rb the following:
RSpec.configure do |config|
...
config.include ApplicationHelper
end
Then test without helper , so it will be:
describe ApplicationHelper do
it 'renders Markdown from plain text' do
plaintext = '# Header'
expect(md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
end
end
From the backtrace, it doesn't look like you're using the rspec-rails gem - just rspec-core and rspec-expectations.
rspec-rails is what provides the helper method to your helper specs. From inside a spec in my codebase:
(rdb:1) self.method(:helper).source_location
["/home/becky/.gem/ruby/2.0.0/gems/rspec-rails-2.14.1/lib/rspec/rails/example/helper_example_group.rb", 19]
I have an Active Record model object called Event, which has many event_things. I want to be able to duplicate the event, so that it gets a new id. I'm using Rails 3.2 and in rails console am able to successfully call
event_copy = event.dup
event_copy.save
However, I also want to duplicate each of the Event's event_things.
copy = event_thing.dup
copy.event_id = event_copy.id
copy.save
But that gives me this error stack:
NoMethodError: undefined method `yaml' for nil:NilClass
from /Users/Ed/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/psych.rb:204:in `dump_stream'
from /Users/Ed/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/psych/core_ext.rb:35:in `psych_y'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:143:in `attribute_change'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `block in changes'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `map'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `changes'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/attribute_methods/dirty.rb:23:in `save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208:in `transaction'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block in save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:240:in `save'
from (irb):18
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
I ran into the same issue as this. It turns out that there's a Kernel.y extension method that is getting in your way, based on this article. Here is what I did to fix my problem:
class Event < ActiveRecord::Base
# alias the attributes
alias_attribute :x_pos, :x
alias_attribute :y_pos, :y
after_initialize :init
# after_initialize will still work, with the aliases
def init
self.x_pos ||= 0
self.y_pos ||= 0
end
# override Kernel.y
def y; end
# override self.y_pos to read out of the :attributes, since we blew away :y
def y_pos
attribute['y']
end
end
After doing this, I was finally able to use my model without having to rename the y database column.
While I normally don't advocate using gems liberally, if you have a lot of relations that need to be preserved in the duplication, or find yourself doing these manual duplications a lot, take a look at deep_clonable.
As for your specific error, try surrounding each line of code with a nil check (one line at a time). I'm sure there's just some case where you're ending up with a nil object that you're overlooking.
I am trying to run ruby on rails code that I got from github and I ended up installing mysql using homebrew and then also ended up installing it directly from http://dev.mysql.com/. It was only then that when I used rake in the application file that the tests began to run. However, now I keep getting this Mysql2::Error: Duplicate entry and I don't understand why. Could it be the fact that I have two mysqls located two different places on my computer? I am not really sure what is causing this problem because all the tables are empty. How can I fix this problem? Thank you for helping. I am new to Ruby on Rails and Mysql.
The stack looks like the following:
HomeControllerTest
test_should_get_index ERROR
Mysql2::Error: Duplicate entry '' for key 'index_admin_users_on_email': INSERT INTO `admin_users` (`created_at`, `updated_at`, `id`) VALUES ('2011-12-09 09:33:30', '2011-12-09 09:33:30', 298486374)
STDERR:
Exception `ActiveRecord::RecordNotUnique' at /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `block in execute'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:302:in `insert_fixture'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:486:in `block (5 levels) in create_fixtures'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:485:in `each'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:485:in `block (4 levels) in create_fixtures'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:484:in `each'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:484:in `block (3 levels) in create_fixtures'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:476:in `each'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:476:in `block (2 levels) in create_fixtures'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:475:in `block in create_fixtures'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:202:in `disable_referential_integrity'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:460:in `create_fixtures'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:924:in `load_fixtures'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:890:in `setup_fixtures'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:413:in `_run_setup_callbacks'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
For anyone else getting this error, this came down to (for me) installing active_admin into an existing project.
The admin_users.yml fixture file was initialised by rails to insert two empty records:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
Commenting out/removing these empty insertions fixed the error for me.
It's unlikely that the two installations of mysql are causing this problem. Rails should only connect to one database. You may not know which one it is connecting to, but it takes extra work to get it to connect to more than one db.
I've seen that duplicate key error before when the fixtures in tests were bad.
In your stack trace, it looks to me like something (fixtures or factories) is trying to create an admin_user without a required field. Twice.
The second time it's throwing an exception because the index is set to unique.
I'm trying to upgrade from 2.3.8 to 3.0.1, but this problem has been plaguing me and preventing further progress for quite some time now. Any attempt to create something from my models (calling Subject.create! from the console, for example) always results in this error:
NoMethodError: undefined method `name' for 0:Fixnum
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `block in create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `each'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `detect'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:26:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/algebra/relations/writes.rb:24:in `call'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/session.rb:17:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/arel-1.0.1/lib/arel/algebra/relations/relation.rb:159:in `insert'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/relation.rb:14:in `insert'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:271:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/timestamp.rb:47:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:281:in `block in create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:413:in `_run_create_callbacks'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:281:in `create'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:247:in `create_or_update'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/callbacks.rb:277:in `block in create_or_update'
from /.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/callbacks.rb:413:in `_run_save_callbacks'
... 1 levels...
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:56:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:49:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:242:in `block in save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:204:in `transaction'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/transactions.rb:242:in `save!'
from /.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:34:in `create!'
from (irb):1
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands/console.rb:44:in `start'
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands/console.rb:8:in `start'
from /.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.1/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'ruby-1.9.2-p0
It would appear Arel is not getting a proper relation to work with, but I am lost as to what is causing this. Any ideas?
Thanks!
Ryan
What version of Ruby are you using? It should be either 1.8.7 or 1.9.2. I've had similar inexplicable issues with 1.9.1.
I would recommend putting some debug statements inside arel. Edit the engine.rb in the last line of the stack trace. I think you will have to restart the console (not just reload!) to see the changes in the arel file.
Here is the offending line in engine.rb:
attribute = relation.record.detect { |attr, _| attr.name.to_s == relation.primary_key.to_s }
Maybe try printing out the attr names and see if anything is passing thru the array detect method. Or print out the class of the "attr". The class should be "Arel::Attribute", but obviously a Fixnum is passing in there somehow.
attribute = relation.record.detect do |attr, _|
p attr.class
p attr.name
attr.name.to_s == relation.primary_key.to_s
end
Just a guess but I am thinking maybe a plugin/gem is interfering with something in your models. Are you using any dependencies that tie directly into ActiveRecord?
Another good exercise would be to create a blank Rails 3 app (with no gems or plugin dependencies) with one model and see if you get the same errors. That would rule out something in your development environment. Then if that works, then maybe you can start removing some of the dependencies one by one from the other app and see if one of them is causing the error.
Hey, I'm using TDD with rails for the first time... interesting concepts. Definitely useful. That is, until I come to this. When I run my test I get:
1) User should build the full name correctly
Failure/Error: #u1.fullname.to_s.should be("#{#attr[:firstname]} #{#attr[:lastname]}")
expected Joe Smith, got "Joe Smith"
# ./spec/models/user_spec.rb:35:in `block (2 levels) in <top (required)>'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/rspec-core-2.0.0.beta.18/lib/rspec/monkey/spork/test_framework/rspec.rb:4:in `run_tests'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:13:in `block in run'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:21:in `block in initialize'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:18:in `fork'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:18:in `initialize'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:9:in `new'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:9:in `run'
# /home/brian/.rvm/gems/ruby-1.9.2-p0#seniorproject/gems/spork-0.8.4/lib/spork/server.rb:47:in `run'
where the test is:
it 'should build the full name correctly' do
#u1.fullname.should be("#{#attr[:firstname]} #{#attr[:lastname]}")
end
and the supporting code is:
def fullname
"#{firstname} #{lastname}"
end
So obviously this works, but what's with the quotation marks? Have I missed something head-smackingly obvious?
Your problem is coming from the fact that you are using be instead of eql. be is expecting a class in the way you've set it up (documentation). Try writing your spec as
#u1.fullname.should eql("#{#attr[:firstname]} #{#attr[:lastname]}")
Documentation for eql
Also, note the difference between eql and the method directly below it in the documentation, equal.