views:

79

answers:

1

I have four tables: jp_properties, cn_properties, jp_dictionaries and cn_dictioanries.

And every jp_property belongs to a jp_dictionary with foreign key "dictionary_id" in table.

Similarly, every cn_property belongs to a cn_dictionary with foreign key "dictionary_id" in table too.

Since there are a lot of same functions in both property model and both dictionary model, I'd like to group all these functions in abstract_class.

The Models are like this:

class Property < AR::Base
  self.abstract_class = true
  belongs_to :dictionry,
             :foreign_key=>'dictionary_id',
             :class=> ---ModelDomainName--- + "Dictionary"

  ### functions shared by JpProperty and CnProperty
end

class Dictionary < AR::Base
  self.abstract_class = true
  has_many :properties,
           :foreign_key=>'dictionary_id',
           :class=> ---ModelDomainName--- + "Dictionary"

  ### functions shared by JpDictionary and CnDictionary
end

class JpProperty < Property
  :set_table_name :jp_properties
end

class CnProperty < Property
  :set_table_name :cn_properties
end

class JpDictionary < Dictionary
  :set_table_name :jp_dictionaries
end

class CnDictionary < Dictionary
  :set_table_name :cn_dictionaries
end

As you can see from the above code, the ---ModelDomainName--- part is either 'Jp' or 'Cn'. And I want to get these string dynamically from the instances of JpProperty, JpDictionary, CnProperty or CnDictionary.

For example:

tempJpProperty = JpProperty.first
tempJpProperty.dictionary #=> will get 'Jp' from tempJpProperty's class name and then apply it to the "belongs_to" declaration.

So the problem is I don't know how to specify the ---ModelDomainName--- part.

More specifically, I have no idea how to get subclass's instance object's class name within the parent class's body.

Can you please help me with this problem?

Edit:

Anybody any ideas?

Basically, what I want to ask is how to 'delay the creation of the association until the subclass is created.'?

A: 

You should be able to do something like this:

class Property < AR::Base
  self.abstract_class = true
  belongs_to :dictionry,
             :foreign_key=>'dictionary_id',
             :class=> #{singlenton_class} + "Dictionary" 

  def singlenton_class
     class << self; self; end 
  end  
end
ez
Thank you for your reply. I tried your suggestion.But I got no luck.
boblu