Forum: Ruby on Rails set_table_name woes, fragmented domain class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9210a60b1492363560375d9cd6c842de?d=identicon&s=25 Michael J. I. Jackson (Guest)
on 2009-03-18 03:42
(Received via mailing list)
Hi all,

I'm running Rails on top of a Mongrel cluster. I have several model
classes that are going to have so many records it is not feasible to
keep them all in the same table. So I've split out the records into
many different tables, each with its own suffix which corresponds to
the id of the model to which all records in that particular table
belong.

For example, if I have a Domain AR object with an id of 20, all
DomainURL objects that :belong_to that particular Domain object are
stored in table domain_urls_20.

This necessitates a call to set_table_name before the DomainURL
objects are fetched, like so:

DomainURL.set_table_name "domain_urls_" + domain.id

However, when I go into production mode, I get mismatched table and
column names on my find's. I am assuming this is caused by
ActiveRecord caching the domain model so that set_table_name is
rendered ineffective when it goes to fetch a record. However, I'm not
quite sure if this is the case.

If anyone could shed some light here, or possibly suggest an
alternative method of accomplishing my objective, I'm all ears.

Thanks,

Michael J. I. Jackson
mjijackson@gmail.com
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2009-03-19 05:25
(Received via mailing list)
Michael J. I. Jackson wrote:

>
> This necessitates a call to set_table_name before the DomainURL
> objects are fetched, like so:
>
> DomainURL.set_table_name "domain_urls_" + domain.id
>
> However, when I go into production mode, I get mismatched table and
> column names on my find's. I am assuming this is caused by
> ActiveRecord caching the domain model so that set_table_name is
> rendered ineffective when it goes to fetch a record. However, I'm not
> quite sure if this is the case.

Perhaps your problem is caching of table names by the urls
association. You could clear the cache using something like

class Domain
   has_many :urls, :class_name => 'DomainURL'

   def set_url_table
     self.class.reflect_on_association(:urls).instance_eval do
       @table_name = @quoted_table_name = nil
     end
     DomainURL.set_table_name "domain_urls_" + id
   end
end

domain = Domain.find(...)
domain.set_url_table
urls = domain.urls

--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
This topic is locked and can not be replied to.