lundi 10 juillet 2017

Namespaced models don't work with polymorphism and I need a workaround

Polymorphic relationships don't work when the models are namespaced in rails 3.1. Here is an example:

class Accounting::Request::Check < ActiveRecord::Base
  has_one :accounting_request, as: :requestable

  def self.table_name
    'accounting_request_checks'
  end
end

class Accounting::Request < ActiveRecord::Base
  belongs_to :requestable, polymorphic: true

  def self.table_name
    'accounting_requests'
  end
end


cr = Accounting::Request::Check.create!()
cr.create_accounting_request

Results in:

NameError: uninitialized constant Accounting::Request::Check::AccountingRequest

My question is, how can I work around this for the time being before we migrate to rails 5?

One solution I found was to add class_name: '::ClassName' but this doesn't work for me.

Aucun commentaire:

Enregistrer un commentaire