Rails minitest & CurrentAttributes, could not find table 'currents' - ruby-on-rails

I'm working on a Rails 7 app, I'm relatively new to Rails testing. Using the default minitest. I'm having some troubles getting my tests to pass / run. When running rails test I'm currently getting the following error:
Error:
CompanyTest#test_Should_have_a_unique_name:
ActiveRecord::StatementInvalid: Could not find table 'currents'
I don't have ny table called Current (or currents) in the app. I'm however having a model class inheriting from CurrentAttributes:
class Current < ActiveSupport::CurrentAttributes
attribute :company
end
I'm getting it's this class that is causing the error when I run rails test. Any ideas on what I'm potentially missing?
One example of a failing test (currently getting 60 errors, so shouldn't be specific to a specific test case):
test "should have a unique name" do
company_b = #company.dup
assert_not company_b.valid?
end
Adding full trace here
Error:
CompanyTest#test_Should_have_a_unique_name:
ActiveRecord::StatementInvalid: Could not find table 'currents'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord- 7.0.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:387:in `table_structure'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord- 7.0.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:116:in `columns'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/schema_cache.rb:117:in `block in columns'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/schema_cache.rb:116:in `fetch'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/schema_cache.rb:116:in `columns'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/schema_cache.rb:125:in `block in columns_hash'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/schema_cache.rb:124:in `fetch'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/schema_cache.rb:124:in `columns_hash'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:472:in `build_fixture_sql'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:142:in `block (2 levels) in build_fixture_statements'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:142:in `map'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:142:in `block in build_fixture_statements'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:140:in `each'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:140:in `flat_map'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/sqlite3/database_statements.rb:140:in `build_fixture_statements'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:402:in `insert_fixtures_set'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/fixtures.rb:630:in `block in insert'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/fixtures.rb:621:in `each'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/fixtures.rb:621:in `insert'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/fixtures.rb:607:in `read_and_insert'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/fixtures.rb:567:in `create_fixtures'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/test_fixtures.rb:275:in `load_fixtures'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/test_fixtures.rb:125:in `setup_fixtures'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activerecord-7.0.4/lib/active_record/test_fixtures.rb:10:in `before_setup'
/Users/<name>/.rvm/gems/ruby-3.1.2/gems/activesupport-7.0.4/lib/active_support/testing/setup_and_teardown.rb:40:in `before_setup'

Related

Rspec + FactoryGirl + Faker tests failing randomly

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 :)

find method ArgumentError "wrong number of arguments (1 for 0)"

In rails console..
Physician.find(1285849521) results in
ArgumentError: wrong number of arguments (1 for 0)
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `initialize'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `new'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `substitute_at'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:989:in `block in create_binds'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:987:in `each'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:987:in `each_with_index'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:987:in `create_binds'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:962:in `build_where'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:586:in `where!'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:576:in `where'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/querying.rb:10:in `where'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/core.rb:150:in `block (2 levels) in find'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/statement_cache.rb:80:in `call'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/statement_cache.rb:80:in `create'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/core.rb:149:in `block in find'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/2.1.0/mutex_m.rb:73:in `synchronize'
... 13 levels...
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `load'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `block in load'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:237:in `load_dependency'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `load'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
and of course..
Physician.find() results in "ActiveRecord::RecordNotFound: Couldn't find Physician without an ID"
My physician.rb is
class Physician < ActiveRecord::Base
self.table_name = "t_phys"
self.primary_key = "sln"
end
I'm using a legacy postgres db, the table t_phys has a natural key called sln, which is a 10 digit integer. There is a physician in the db with sln 1285849521 (the integer in the find method above), which I know because Physician.all() works properly and that's the first record that is displayed.
What am I doing wrong? How can I get the find method to work properly? I'm just getting started with rails again, probably a newbie mistake somewhere. Appreciate the help!
For more detail, when I got to localhost:3000/physicians/1285849521, the page renders:
ArgumentError in PhysiciansController#show
Wrong number of arguments (1 for 0)
Extracted source (around line #160):
159 def substitute_at(column, index)
160 Arel::Nodes::BindParam.new "$#{index + 1}"
161 end
162
163 def exec_query(sql, name = 'SQL', binds = [])
I can provide the full stack trace if necessary.
Seems like it should work, but I'm wondering if the custom primary key is still messing with find.
Maybe this background will help confirm one way or the other:
http://ruby-journal.com/how-to-override-default-primary-key-id-in-rails/
Thanks to Todd Agulnick's comment above, I updated to Rails 4.2 rc2 and the error went away. I encountered a new error, which I resolved and is outside the scope of this question. Thanks Todd!

Mysql2::Error: Duplicate entry '' for key 'index_admin_users_on_email' Ruby on Rails Error

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.

Upgrading Rails 2.3.8 app to Rails 3.0.1, cannot call create on any models

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.

Where does a rails (3) application define what is supposed to happen in activesupport-3.0.0.rc/lib/active_support/testing/setup_and_teardown.rb?

I generated a model file but then chose to let it single table inherit from another model. I had to run no migration, because the columns all were already there.
Now whenever I want to run tests, I get complaints that the table for the model does not exist.
So I have Product < Article < ActiveRecord::Base, product has no own table (does not have it in the schema neither), but still I get
test_should_update_article(ArticlesControllerTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: products: DELETE FROM "products" WHERE 1=1
AR/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log'
AR/active_record/connection_adapters/abstract_adapter.rb:194:in `log'
AR/active_record/connection_adapters/sqlite_adapter.rb:135:in `execute'
AR/active_record/connection_adapters/abstract/database_statements.rb:269:in `update_sql'
AR/active_record/connection_adapters/sqlite_adapter.rb:139:in `update_sql'
AR/active_record/connection_adapters/abstract/database_statements.rb:274:in `delete_sql'
AR/active_record/connection_adapters/sqlite_adapter.rb:145:in `delete_sql'
AR/active_record/connection_adapters/abstract/database_statements.rb:54:in `delete'
AR/active_record/connection_adapters/abstract/query_cache.rb:16:in `delete'
AR/active_record/fixtures.rb:551:in `delete_existing_fixtures'
AR/active_record/fixtures.rb:513:in `block (4 levels) in create_fixtures'
AR/active_record/fixtures.rb:513:in `each'
AR/active_record/fixtures.rb:513:in `block (3 levels) in create_fixtures'
AR/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
AR/active_record/fixtures.rb:512:in `block (2 levels) in create_fixtures'
AR/active_record/connection_adapters/abstract_adapter.rb:104:in `disable_referential_integrity'
AR/active_record/fixtures.rb:503:in `block in create_fixtures'
AS/active_support/benchmarkable.rb:55:in `silence'
AR/active_record/fixtures.rb:502:in `create_fixtures'
AR/active_record/fixtures.rb:961:in `load_fixtures'
AR/active_record/fixtures.rb:926:in `setup_fixtures'
AS/active_support/callbacks.rb:419:in `_run_setup_callbacks'
AS/active_support/testing/setup_and_teardown.rb:34:in `run'
AR is a shorthand for /Users/janlimpens/.rvm/AS/ruby-1.9.2-rc2#rails3/AS/activerecord-3.0.0.rc/lib
AS for /Users/janlimpens/.rvm/AS/ruby-1.9.2-rc2#rails3/AS/activesupport-3.0.0.rc/lib/
I wonder what I could do...
Assuming you are using Single Table Inheritance, as Product model inherits from Article Model.
For this to work you need a "type" column in Article Table, which will be filled by "Product"
or any other class that inherits Article model.
Add that and the sql query would look like
select * from articles where articles.type = "Product"

Resources