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?
Michael Guterl
2009-Aug-09 20:30 UTC
Re: active record multiple table inheritance with abstract parent
boblu wrote:> 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?This is untested but worth a try. class Property < AR::Base self.abstract_class = true def self.inherited(klass) klass.to_s =~ /(\w+)[A-Z].*/ language = $1 klass.belongs_to :dictionary, :foreign_key => ''dictionary_id'', :class => "#{language}Dictionary" end end class Dictionary < AR::Base self.abstract_class = true def self.inherited(klass) klass.to_s =~ /(\w+)[A-Z].*/ language = $1 klass.has_many :properties, :foreign_key => ''dictionary_id'', :class => "#{language}Property" end end Basically you have to delay the creation of the association until the subclass is created. Best, Michael Guterl -- Posted via http://www.ruby-forum.com/.
boblu
2009-Aug-10 00:27 UTC
Re: active record multiple table inheritance with abstract parent
Thanks a lot for your reply. I just tried your suggestion, but I did not get luck. This is the console output: Jp::Property.first TypeError: can''t dup NilClass from /rails/activerecord/lib/active_record/base.rb:2189:in `dup'' from /rails/activerecord/lib/active_record/base.rb:2189:in `scoped_methods'' from /rails/activerecord/lib/active_record/base.rb:2193:in `current_scoped_methods'' from /rails/activerecord/lib/active_record/base.rb:2183:in `scope'' from /rails/activerecord/lib/active_record/base.rb:2414:in `set_readonly_option!'' from /rails/activerecord/lib/active_record/base.rb:610:in `find'' from /rails/activerecord/lib/active_record/base.rb:623:in `first'' from (irb):2 Is there any other way to ''delay the creation of the association until the subclass is created. ''? Thank you On 8月10日, 午前5:30, Michael Guterl <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> bobluwrote: > > 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? > > This is untested but worth a try. > > class Property < AR::Base > self.abstract_class = true > def self.inherited(klass) > klass.to_s =~ /(\w+)[A-Z].*/ > language = $1 > klass.belongs_to :dictionary, :foreign_key => ''dictionary_id'', > :class => "#{language}Dictionary" > end > end > > class Dictionary < AR::Base > self.abstract_class = true > def self.inherited(klass) > klass.to_s =~ /(\w+)[A-Z].*/ > language = $1 > klass.has_many :properties, :foreign_key => ''dictionary_id'', > :class => "#{language}Property" > end > end > > Basically you have to delay the creation of the association until the > subclass is created. > > Best, > Michael Guterl > -- > Posted viahttp://www.ruby-forum.com/.
boblu
2009-Aug-10 13:19 UTC
Re: active record multiple table inheritance with abstract parent
anyboby any ideas? On 8月10日, 午前9:27, boblu <bobl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks a lot for your reply. > > I just tried your suggestion, but I did not get luck. > > This is the console output: > > Jp::Property.first > TypeError: can''t dup NilClass > from /rails/activerecord/lib/active_record/base.rb:2189:in `dup'' > from /rails/activerecord/lib/active_record/base.rb:2189:in > `scoped_methods'' > from /rails/activerecord/lib/active_record/base.rb:2193:in > `current_scoped_methods'' > from /rails/activerecord/lib/active_record/base.rb:2183:in `scope'' > from /rails/activerecord/lib/active_record/base.rb:2414:in > `set_readonly_option!'' > from /rails/activerecord/lib/active_record/base.rb:610:in `find'' > from /rails/activerecord/lib/active_record/base.rb:623:in `first'' > from (irb):2 > > Is there any other way to ''delay the creation of the association until > the subclass is created. ''? > > Thank you > > On 8月10日, 午前5:30, Michael Guterl <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > bobluwrote: > > > 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? > > > This is untested but worth a try. > > > class Property < AR::Base > > self.abstract_class = true > > def self.inherited(klass) > > klass.to_s =~ /(\w+)[A-Z].*/ > > language = $1 > > klass.belongs_to :dictionary, :foreign_key => ''dictionary_id'', > > :class => "#{language}Dictionary" > > end > > end > > > class Dictionary < AR::Base > > self.abstract_class = true > > def self.inherited(klass) > > klass.to_s =~ /(\w+)[A-Z].*/ > > language = $1 > > klass.has_many :properties, :foreign_key => ''dictionary_id'', > > :class => "#{language}Property" > > end > > end > > > Basically you have to delay the creation of the association until the > > subclass is created. > > > Best, > > Michael Guterl > > -- > > Posted viahttp://www.ruby-forum.com/.
Ilan Berci
2009-Aug-10 13:27 UTC
Re: active record multiple table inheritance with abstract parent
boblu wrote:> anyboby any ideas?self.class.name() doesn''t work in the super? -- Posted via http://www.ruby-forum.com/.
boblu
2009-Aug-10 14:08 UTC
Re: active record multiple table inheritance with abstract parent
To IIan Can you tell me what do you mean? If you mean whether I tried the following class Property < AR::Base self.abstract_class = true belongs_to :dictionary, :foreign_key => ''dictionary_id'', :class => "# {self.class.name.to_s[0..1]}Dictionary" end I can tell you it does not work, because the ''belongs_to'' declaration will be evaluated in the abstract class and the sub class will only inherit the methods generated by ''belongs_to''. On 8月10日, 午後10:27, Ilan Berci <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> boblu wrote: > > anyboby any ideas? > > self.class.name() doesn''t work in the super? > > -- > Posted viahttp://www.ruby-forum.com/.