I've spent half a working day trying to track this down in AR. Given a model setup like:
class Publication < ActiveRecord::Base
has_many :subscriptions
end
class Subscription < ActiveRecord::Base
belongs_to :publication
belongs_to :user
end
In controller,
@new_subscription = publication.subscriptions.create( user: @current_user ) { |r| ... }
Works perfectly in development mode first time it is invoked after a server start. Second time however, it throws an error from
~/.rvm/gems/ruby-1.9.1-p378/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `block in assign_attributes':
ActiveRecord::UnknownAttributeError: unknown attribute: user
Setting
config.cache_classes = true
in config/environments/development.cfg
makes the problem disappear.
Where does AR handle creating the attr_accessor
/writer for a belongs_to
association, and why would this work the first but not the second time invoked? Seems like the accessor is not getting defined when the class is unloaded and reloaded between requests? Further information: the Publication
and the Subscription
are using STI, and the associations are defined on the base classes if that shouldn't make any difference. This is ruby 1.9.1 and Rails/AR 2.3.8
Grateful for any clues.