In Ruby on Rails, how do I make a polymorphic model associated with a model with names?

I have the following models.

# app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name => "Link", :as => :from, :dependent => :destroy end # app/models/link.rb class Link < ActiveRecord::Base belongs_to :from, :polymorphic => true belongs_to :object_value, :polymorphic => true end 

The problem is that when I do the following, from_type is not the domain namespace prefix for the model, for example.

  Domain::DomainObject.all(:include=> :links_from ) 

This calls the following SELECT:

  SELECT `links`.* FROM `links` WHERE (`links`.`from_id` IN (5,6,12,13,18,24,25,27,29,30,31,32,34,35,39) and `links`.`from_type` = 'DomainObject') 

The request should be:

  SELECT `links`.* FROM `links` WHERE (`links`.`from_id` IN (5,6,12,13,18,24,25,27,29,30,31,32,34,35,39) and `links`.`from_type` = 'Domain::DomainObject') 

because Rails automatically saves a model with a namespace.

I saw several recommendations on Rails sites on how to do this:

  belongs_to :from, :polymorphic => true, :class_name => "Domain::DomainObject" 

However, this does not work either.

So, is there a better way to do this? Or is it not supported?

+4
source share
2 answers

To fix this, I did include Domain in the DomainObject model and set ActiveRecord::Base.store_full_sti_class = true in config/environment.rb .

+5
source

hoyhoy response is the solution. This also solved my problem (I really wanted the namespace to be disabled).

However, I would recommend adding x.store_full_sti_class = true to config/environment.rb only if this is desired globally. I assume that this may not always be mandatory, in which case we can easily transfer the solution to the class level.

 class User < ActiveRecord::Base self.store_full_sti_class = true ... end 
+1
source

All Articles