我有以下型号.
# 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
问题是,当我执行以下操作时,from_type不会将Domain命名空间添加到模型前面,例如
Domain::DomainObject.all(:include=> :links_from )
这会导致以下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')
查询应该是:
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')
因为Rails会自动使用命名空间保存模型.
我在Rails网站上看到过一些关于这样做的建议:
belongs_to :from, :polymorphic => true, :class_name => "Domain::DomainObject"
但是,这似乎也不起作用.
那么,有更好的方法吗?或者这不受支持?
为了解决这个问题,我include Domain
在DomainObject
模型中做了一个并ActiveRecord::Base.store_full_sti_class = true
进入了config/environment.rb
.