rails/rails — Issue #4882: self.table_name problems with 'find_or_create_by_'
Author: ened
Created: 2012-02-05 07:50:05+00:00
State: open

Given my model looks like this:

``` ruby
class APN::Device < APN::Base
self.table_name = 'apn_devices'

has_many :notifications, :class_name => 'APN::Notification'
```

With 1 column `token:string`. Its base class looks like this:

``` ruby
module APN
class Base < ActiveRecord::Base # :nodoc:

# def self.table_name # :nodoc:
# self.to_s.gsub("::", "_").tableize
# end

end
end
```

I already tried commenting in/out the `self.table_name` method and the `self.table_name =` calls in the child classes.

Then I can use this method to find a Device by token:

```
ruby-1.9.2-p290 :004 > APN::Device.find_by_token('xxxx xxxx xxxx xxxx')
APN::Device Load (0.5ms) SELECT "apn_devices".* FROM "apn_devices" WHERE "apn_devices"."token" = 'xxxx xxxx xxxx xxxx' LIMIT 1
=> nil
```

But once I try to use `find_or_create_by_token`:

```
ruby-1.9.2-p290 :005 > APN::Device.find_or_create_by_token('xxxx xxxx xxxx xxxx')
APN::Device Load (0.5ms) SELECT "apn_devices".* FROM "apn_devices" WHERE "apn_devices"."token" = 'xxxx xxxx xxxx xxxx' LIMIT 1
ActiveRecord::StatementInvalid: PGError: ERROR: relation "bases" does not exist
LINE 4: WHERE a.attrelid = '"bases"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"bases"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum

from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1106:in `async_exec'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1106:in `exec_no_cache'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `block in exec_query'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:649:in `exec_query'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1231:in `column_definitions'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:845:in `columns'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/model_schema.rb:228:in `yield'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/model_schema.rb:228:in `default'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/model_schema.rb:228:in `columns'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/model_schema.rb:248:in `column_names'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_methods.rb:44:in `block in define_attribute_methods'
from :10:in `synchronize'
... 3 levels...
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_methods.rb:41:in `define_attribute_methods'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_methods.rb:145:in `respond_to?'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_assignment.rb:81:in `block in assign_attributes'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_assignment.rb:78:in `each'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:290:in `block in find_or_instantiator_by_attributes'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/base.rb:497:in `initialize'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:289:in `new'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:289:in `find_or_instantiator_by_attributes'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/dynamic_matchers.rb:47:in `method_missing'
from (irb):5
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /Users/ened/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands.rb:41:in `'
from script/rails:6:in `require'
from script/rails:6:in `

'
```

All of this is happening with Rails3.2 and Ruby1.9.2. `bundle list`:
Gems included by the bundle:

```
* RedCloth (4.2.9)
* ZenTest (4.5.0)
* actionmailer (3.2.1)
* actionpack (3.2.1)
* activemodel (3.2.1)
* activerecord (3.2.1)
* activeresource (3.2.1)
* activesupport (3.2.1)
* apn_on_rails (0.3.1rails3 fdbe81b)
* arel (3.0.0)
* autotest (4.4.6)
* autotest-growl (0.2.16)
* autotest-rails (4.1.1)
* bcrypt-ruby (3.0.1)
* builder (3.0.0)
* bundler (1.0.21)
* cancan (1.6.7)
* capistrano (2.9.0)
* carrierwave (0.5.8)
* coffee-rails (3.2.2)
* coffee-script (2.2.0)
* coffee-script-source (1.2.0)
* configatron (2.9.0)
* devise (2.0.0)
* diff-lcs (1.1.3)
* erubis (2.7.0)
* exception_notification_rails3 (1.2.0)
* execjs (1.3.0)
* ffi (1.0.11)
* guard (1.0.0)
* guard-rspec (0.6.0)
* haml (3.1.4)
* haml-rails (0.3.4)
* highline (1.6.11)
* hike (1.2.1)
* i18n (0.6.0)
* irwi (0.4.1 b78694c)
* journey (1.0.1)
* jquery-rails (2.0.0)
* json (1.6.5)
* libv8 (3.3.10.4)
* machinist (2.0)
* mail (2.4.1)
* metaclass (0.0.1)
* mime-types (1.17.2)
* mini_magick (3.4)
* mocha (0.10.3)
* multi_json (1.0.4)
* net-scp (1.0.4)
* net-sftp (2.0.5)
* net-ssh (2.3.0)
* net-ssh-gateway (1.1.0)
* nifty-generators (0.4.6)
* orm_adapter (0.0.6)
* pg (0.12.2)
* polyamorous (0.5.0)
* polyglot (0.3.3)
* rack (1.4.1)
* rack-cache (1.1)
* rack-ssl (1.3.2)
* rack-test (0.6.1)
* rails (3.2.1)
* rails_autolink (1.0.5)
* railties (3.2.1)
* rake (0.9.2.2)
* ransack (0.6.0)
* rdoc (3.12)
* rspec (2.8.0)
* rspec-core (2.8.0)
* rspec-expectations (2.8.0)
* rspec-mocks (2.8.0)
* rspec-rails (2.8.1)
* rubyzip (0.9.5)
* sass (3.1.12)
* sass-rails (3.2.4)
* simplecov (0.5.4)
* simplecov-html (0.5.3)
* sprockets (2.1.2)
* sqlite3 (1.3.5)
* sqlite3-ruby (1.3.3)
* subexec (0.2.1)
* therubyracer (0.9.9)
* thor (0.14.6)
* tilt (1.3.3)
* treetop (1.4.10)
* tzinfo (0.3.31)
* uglifier (1.2.3)
* warden (1.1.0)
* yamler (0.1.0)
```

I didn't test it further, in order to pinpoint it to the postgresql adapter. It seems to me more like a find_or_create_by_ issue?