First of all, sorry for the crossposting, but I put this into the Ruby Forum first of all, but was pointed to this as a more appropriate location. I''m using ActiveRecord and ActiveSupport in a non-rails environment to connect to multiple databases, and I''ve found the following (single database) to cause me an error. Note that params is my database settings and omitted for obvious reasons. pages, components, and books are all tables in the same db. require ''active_record'' module TestDB class Inherited < ActiveRecord::Base self.logger self.default_timezone :utc self.establish_connection(params) end class Page < Inherited puts self.name puts self.table_name end class Book < ActiveRecord::Base puts self.name puts self.table_name end end class Component < ActiveRecord::Base puts self.name puts self.table_name end outputs: TestDB::Page <= expected inheriteds <= Hmm, table name for the parent? TestDB::Book <= also expected books <= Seems to be using the Inherited connection Component <= Yep, no module name components <= also using the Inherited connection Is this expected behaviour from an inherited class. Is there anyway to scope the ActiveRecord connection without resorting to plugins or other gems? Mac -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 26, 5:51 pm, Paul Mckibbin <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> First of all, sorry for the crossposting, but I put this into the Ruby > Forum first of all, but was pointed to this as a more appropriate > location. > > I''m using ActiveRecord and ActiveSupport in a non-rails environment > to connect to multiple databases, and I''ve found the following (single > database) to cause me an error. Note that params is my database settings > and omitted for obvious reasons. pages, components, and books are all > tables in the same db. > > require ''active_record'' > module TestDB > class Inherited < ActiveRecord::Base > self.logger > self.default_timezone :utc > self.establish_connection(params)You should set self.abstract_class = true on this class or activerecord might think you''re trying single table inheritance. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Jan 26, 5:51�pm, Paul Mckibbin <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> require ''active_record'' >> module TestDB >> � class Inherited < ActiveRecord::Base >> � � self.logger >> � � self.default_timezone :utc >> � � self.establish_connection(params) > > You should set self.abstract_class = true on this class or > activerecord might think you''re trying single table inheritance. > > FredI''ve used inheritance with many of my models. To expand upon what Fred is saying here, you really need to do two things. I''ll show you an example use and then you can configure your own class to use it: One of my models is an inheritance template: class InheritanceTemplate < ActiveRecord::Base self.abstract_class = true end Other models inherit from this template by doing the following: class MyPage < InheritanceTemplate set_table_name "my_pages" #pluralized table name end etc. Notice in the classes that are using the inheritance template you should also set the table name so that there''s no confusion going on in the templates. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 26, 8:12 pm, Alpha Blue <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > Notice in the classes that are using the inheritance template you should > also set the table name so that there''s no confusion going on in the > templates.I''ve never needed to do that. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Jan 26, 8:12�pm, Alpha Blue <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> Notice in the classes that are using the inheritance template you should >> also set the table name so that there''s no confusion going on in the >> templates. > > I''ve never needed to do that. > > FredIt depends on what he''s going to do with the code. In my case, I have several archive tables that apply to some of my inheritance models. I only placed it in there as an example. :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alpha Blue wrote:> Frederick Cheung wrote: >> On Jan 26, 5:51�pm, Paul Mckibbin <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > class MyPage < InheritanceTemplate > set_table_name "my_pages" #pluralized table name > end > > etc. > > Notice in the classes that are using the inheritance template you should > also set the table name so that there''s no confusion going on in the > templates.I was doing that in the models that I had inherited, but if there isn''t a way to do it without setting the table name then I don''t see the benefit. Paul -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Paul Mckibbin wrote:> Alpha Blue wrote: >> Frederick Cheung wrote: >>> On Jan 26, 5:51�pm, Paul Mckibbin <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> class MyPage < InheritanceTemplate >> set_table_name "my_pages" #pluralized table name >> end >> >> etc. >> >> Notice in the classes that are using the inheritance template you should >> also set the table name so that there''s no confusion going on in the >> templates. > > I was doing that in the models that I had inherited, but if there isn''t > a way to do it without setting the table name then I don''t see the > benefit.As Fred said, setting the table name is probably unnecessary.> > PaulBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.