ruby on rails 3 - ActiveRecord::ConfigurationError - Association named 'whatever' was not found -
i have complicated case i'm developing rails 3 engine , intermittently error in title. here's stacktrace:
activerecord::configurationerror - association named 'whatever' not found; perhaps misspelled it?: activerecord (3.2.18) lib/active_record/associations/preloader.rb:150:in `block in records_by_reflection' activerecord (3.2.18) lib/active_record/associations/preloader.rb:146:in `records_by_reflection' activerecord (3.2.18) lib/active_record/associations/preloader.rb:139:in `grouped_records' activerecord (3.2.18) lib/active_record/associations/preloader.rb:130:in `preload_one' activerecord (3.2.18) lib/active_record/associations/preloader.rb:109:in `preload' activerecord (3.2.18) lib/active_record/associations/preloader.rb:98:in `block in run' activerecord (3.2.18) lib/active_record/associations/preloader.rb:98:in `run' activerecord (3.2.18) lib/active_record/relation.rb:181:in `block in exec_queries' activerecord (3.2.18) lib/active_record/relation.rb:180:in `exec_queries' activerecord (3.2.18) lib/active_record/relation.rb:160:in `block in to_a' activerecord (3.2.18) lib/active_record/explain.rb:41:in `logging_query_plan' activerecord (3.2.18) lib/active_record/relation.rb:159:in `to_a' activerecord (3.2.18) lib/active_record/relation/delegation.rb:39:in `+' /users/me/src/appointment_engine/app/controllers/appointment_engine/appointments_controller.rb:42:in `block (3 levels) in index' /users/me/src/appointment_engine/app/controllers/appointment_engine/appointments_controller.rb:41:in `block (2 levels) in index' actionpack (3.2.18) lib/action_controller/metal/mime_responds.rb:196:in `respond_to' /users/me/src/appointment_engine/app/controllers/appointment_engine/appointments_controller.rb:12:in `index'
to summarize: there's model named appointment
in engine polymorphically associated has_many :through
host app's user
model (this requirement because associate model). here's has_many
declaration in appointment
class appointment < activerecord::base has_many :scheduleables, through: :appointments_scheduleables, source_type: kae.scheduleable_class.name has_many :schedulers, through: :appointments_schedulers, source_type: kae.scheduler_class.name end
here ran first problem; need set :source_type
on has_many :through
polymorphic associations (it doesn't work without it) , need know class of associated model but, when engine's appointment
model loads before host app's user
model loads , therefore engine's module kae
hasn't received value kae.scheduleable_class
yet.
here's how kae
receives value:
# in host app class user < activerecord::base acts_as_scheduler end
i wrote acts_as_scheduler
ar mixin, declare has_many :through
association appointment
.
my first attempt fix this: put appointment
's has_many
declaration in hook inside railtie:
activesupport.on_load :after_initialize kae::appointment.class_eval has_many :scheduleables, through: :appointments_scheduleables, source_type: kae.scheduleable_class.name has_many :schedulers, through: :appointments_schedulers, source_type: kae.scheduler_class.name end end
ok works, wait till host app loads , have values kae.scheduleable_class
, kae.scheduler_class
, great.
except error in title intermittently!
i can boot fine, use app while (10-30 mins) , out of boom! i've tried under rails server
, thin
, , unicorn
; same must app/framework level bug. in active record preloader class top of stacktrace points to:
# lib/active_record/associations/preloader.rb:150 def records_by_reflection(association) records.group_by |record| reflection = record.class.reflections[association] unless reflection raise activerecord::configurationerror, "association named '#{association}' not found; " \ "perhaps misspelled it?" end reflection end end
how can model forget of it's associations?
so i'm doing directly in appointment
model , seems working far it's ugly:
class appointment < activerecord::base if kae.scheduler_class && kae.scheduleable_class has_many :scheduleables, through: :appointments_scheduleables, source_type: kae.scheduleable_class.name has_many :schedulers, through: :appointments_schedulers, source_type: kae.scheduler_class.name else binding.pry # todo activesupport.on_load :after_initialize kae::appointment.class_eval has_many :scheduleables, through: :appointments_scheduleables, source_type: kae.scheduleable_class.name has_many :schedulers, through: :appointments_schedulers, source_type: kae.scheduler_class.name end end end end
anybody know of better way of declaring has_many :through
polymorphic associations in rails 3 engine?
i've been looking @ open source projects acts_as_taggable_on_steroids
, paper_trail
see how associations don't have polymorphic has_many :through
.
anybody know projects have type of association?
load ordering can pain rails autoloading+auto-reloading magic. in these situations make dependencies more explicit. suggest try using rails's require_dependency
in host ensure doesn't occur:
# in host app require_dependency 'appointment' class user < activerecord::base acts_as_scheduler end
it's activesupport's version of require
plays development environment's auto-reloading, why you're experiencing issue intermittently when working on application.
Comments
Post a Comment