Observers, models, edge, chickens and eggs


#1

Imagine the following application:

config/environment.rb:


config.active_record.observers = :user_observer

app/models/user.rb:

class User < ActiveRecord::Base
MY_CONST = Other::ITS_CONST
end

app/models/other.rb:

class Other < ActiveRecord::Base
ITS_CONST = 1
end

app/models/user_observer.rb:

class UserObserver < ActiveRecord::Observer
end


This works fine as long as the “users” and “others” tables are already
created. However, if they’re not, you can’t actually run the migration
to
create those tables, because rails dies during initialization:

/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:135:in
`log’:ActiveRecord::StatementInvalid: RuntimeError: ERROR C42P01
Mrelation “users” does not exist Fnamespace.c L200
RRangeVarGetRelid: 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 = ‘users’::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum

This is on Cygwin with Postgres running edge rails, but I get a similar
result on a Mac with MySql (it uses “SHOW FIELDS FROM” to get the column
names instead).

The full stack is below. Anyone have any suggestions?

RuntimeError: ERROR C42P01 Mrelation “users” does not exist
Fnamespace.c L200 RRangeVarGetRelid: 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 = ‘users’::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:135:in
log' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:150:inquery’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:537:in
column_definitions' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:231:incolumns’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/base.rb:842:in
columns' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/base.rb:916:ininspect’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/…/…/activesupport/lib/active_support/core_ext/symbol.rb:10:in
__send__' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/core_ext/symbol.rb:10:into_proc’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/…/…/activesupport/lib/active_support/dependencies.rb:427:in
collect' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:427:inlog_call’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/…/…/activesupport/lib/active_support/dependencies.rb:222:in
load_missing_constant' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:468:inconst_missing’
/cygdrive/c/dev/src/tmp/test_observers/app/models/user.rb:2
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/…/…/activesupport/lib/active_support/dependencies.rb:203:in
load_without_new_constant_marking' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:203:inload_file’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/…/…/activesupport/lib/active_support/dependencies.rb:342:in
new_constants_in' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:202:inload_file’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/…/…/activesupport/lib/active_support/dependencies.rb:94:in
require_or_load' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:248:inload_missing_constant’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/…/…/activesupport/lib/active_support/dependencies.rb:452:in
const_missing' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:464:inconst_missing’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/…/…/activesupport/lib/active_support/inflector.rb:257:in
constantize' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/core_ext/string/inflections.rb:148:inconstantize’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/observer.rb:144:in
observed_class' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/observer.rb:170:inobserved_classes’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/observer.rb:153:in
initialize' /usr/lib/ruby/1.8/singleton.rb:95:innew’
/usr/lib/ruby/1.8/singleton.rb:95:in instance' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/observer.rb:38:ininstantiate_observers’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/observer.rb:36:in
each' /cygdrive/c/dev/src/tmp/test_observers/vendor/rails/activerecord/lib/active_record/observer.rb:36:ininstantiate_observers’
/cygdrive/c/dev/src/tmp/test_observers/config/…/vendor/rails/railties/lib/initializer.rb:210:in
load_observers' /cygdrive/c/dev/src/tmp/test_observers/config/../vendor/rails/railties/lib/initializer.rb:112:inprocess’
/cygdrive/c/dev/src/tmp/test_observers/config/…/vendor/rails/railties/lib/initializer.rb:49:in
send' /cygdrive/c/dev/src/tmp/test_observers/config/../vendor/rails/railties/lib/initializer.rb:49:inrun’
/cygdrive/c/dev/src/tmp/test_observers/config/environment.rb:13
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
/cygdrive/c/dev/src/tmp/test_observers/vendor/rails/railties/lib/tasks/misc.rake:3
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in call' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:inexecute’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in each' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:inexecute’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in invoke' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:insynchronize’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in invoke' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:369:ininvoke_prerequisites’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in each' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:insend’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1003:in each' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:368:ininvoke_prerequisites’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:361:in invoke' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:insynchronize’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in invoke' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:intop_level’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in each' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:intop_level’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in
standard_exception_handling' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:intop_level’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1711:in run' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:instandard_exception_handling’
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:in run' /usr/lib/ruby/gems/1.8/gems/rake-0.7.3/bin/rake:7 /usr/bin/rake:16:inload’
/usr/bin/rake:16