Search code examples
ruby-on-railsrubypolymorphic-associationsmodel-associations

Rails Polymorphic associations with name space


I want to save different results(default and manual), each result can have a reason. Thought that this would be a good place for a polymorphic association. The Models are namespaced however and this is prooving to be tricker than anticipated. following the guide

app/models/event/reason.rb

#  id              :integer          not null, primary key
#  reasons         :string
#  reasonable_id   :integer
#  reasonable_type :string
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#

class Event::Reason < ActiveRecord::Base
  belongs_to :reasonable, polymorphic: true
end

app/models/event/result.rb

class Event::Result < ActiveRecord::Base
  belongs_to :event
  has_one :event_reason, as: :reasonable
end

app/models/event/manual_result.rb

class Event::ManualResult < ActiveRecord::Base
  belongs_to :event
  has_one :event_reason, as: :reasonable
end

But if I try do something like:

Event::ManualResult.last.event_reason
  Event::ManualResult Load (5.1ms)  SELECT  "event_manual_results".* FROM "event_manual_results"  ORDER BY "event_manual_results"."id" DESC LIMIT 1
  NameError: uninitialized constant Event::ManualResult::EventReason

or

 Event::Result.last.event_reason
   Event::Result Load (0.4ms)  SELECT  "event_results".* FROM "event_results"  ORDER BY "event_results"."id" DESC LIMIT 1
   NameError: uninitialized constant Event::Result::EventReason

It would seem it is expecting the associations to be nested within an additonal layer Event::ManualResult::EventReason and Event::Result::EventReason


Solution

  • You just need to specify the class_name on the association:

    class Event::Result < ActiveRecord::Base
      belongs_to :event
      has_one :event_reason, as: :reasonable, class_name: 'Event::Reason'
    end
    

    This way you don't allow rails to try and guess the class for you from .event_reason (which it is unable to do in this type of case).