Spent a working day on this.
I have
class Box
has_many :users, :through => :subscriptions
end
I also have custom insert_
You're seeing the effects of the ActiveRecord SQL query cache.
Either wrap the pre-INSERT references to the users association in a call to uncached
self.class.uncached do
# ...
end
This will stop ActiveRecord from caching the results of any queries inside the block. If you have code in many places, this may be annoying.
You can also clear the cache after you are done with your inserts by calling connection.clear_query_cache.