Hi, Lets suppose a Resource and Relation model exist and Article and a Comment inherit from Resource and an ArticleComment defines a relationship. When everything is setup and you create your instances and save them this is what is saved in the database.
RESOURCES
id | description | type 14 | NULL | SpotArticle 15 | NULL | Comment
RELATIONS
id | source_resource_id | target_resource_id | type source_resource_type | target_resource_type 4 | 14 | 15 |
ArticleComment | SpotArticle | Comment
At least this is what I would like to have in the database. But the source_resource_type and target_resource_type both contain "Resource" instead. Looking at the rails code I saw in "ActiveRecord::Associations::AssociationProxy.set_belongs_to_association_for" that the Class.base_name is stored instead of the Class.name and this results in the base class name Resource.
Is there anybody who knows why the Class.base_name is used here? Is there a better way of doing this? Without having to override set_belongs_to_association_for?
Mehmet
Setup stuff
def self.up create_table :relations do |t| t.integer :source_resource_id, :target_resource_id t.string :source_resource_type, :target_resource_type, :type t.timestamps end
create_table :resources do |t| t.string :type; :description t.timestamps end end
class Resource < ActiveRecord::Base end
class Relation < ActiveRecord::Base end
class Article < Resource end
class SpotArticle < Article has_many :article_comments, :as => :source_resource, :dependent => :destroy end
class Comment < Resource has_many :article_comments, :as => :target_resource, :dependent => :destroy end
class ArticleComment < Relation belongs_to :source_resource, :polymorphic => true, :class_name => 'SpotArticle' belongs_to :target_resource, :polymorphic => true, :class_name => 'Comment' end