Summary / Error
I get this error in different places of my application:
ActiveRecord::AssociationTypeMismatch in Settings::CompaniesController
Controller
The controller looks like this, but a problem can occur at any time when a company model is used to save or update another model:
class Settings::CompaniesController < SettingsController def show @company = current_user.company @classification = Classification.new(company: @company) end def update end end
Facts / Observations
Some facts and observations:
- The problem occurs randomly, but usually after the development server has been running for a while.
- The problem does not arise in production.
- The problem arises even when I did not make changes to the
Company model at all. - The problem is solved by restarting the server.
Theory
As far as I understand, this is due to the dynamic loading of classes.
Somehow the company class gets a new class identifier when it reloads. I heard rumors about it due to sloppiness. I do not require myself in the company model, but I use active-record-postgres-hstore .
Models
This is a Company model:
class Company < ActiveRecord::Base serialize :preferences, ActiveRecord::Coders::Hstore DEFAULT_PREFERENCES = { require_review: false } has_many :users has_many :challenges has_many :ideas has_many :criteria has_many :classifications attr_accessible :contact_email, :contact_name, :contact_phone, :email, :logotype_id, :name, :phone, :classifications_attributes, :criteria_attributes, :preferences accepts_nested_attributes_for :criteria accepts_nested_attributes_for :classifications after_create :setup before_save :set_slug
Classification Model:
class Classification < ActiveRecord::Base attr_accessible :description, :name, :company, :company_id has_many :ideas belongs_to :company def to_s name end end
Factual question
I would be interested to know why this problem occurs, and if it can be somehow avoided.
I know what exception means in principle. I want to know how to avoid this.
In particular, I would like to know if I caused the problem somehow, or if it is a gem, and in this case, if I could somehow fix the stone.
Thank you in advance for any answers.