On 1/27/07, Russell N. [email protected] wrote:
the specific model as you might think. Lots of fun confusion on that for me.
#{table_name}_othermodels.* FROM #{table_name}_othermodels WHERE
RSL
Hmm, I posted a reply using the Google G. interface and it’s not
appearing (even though my first reply to this post came through just
fine).
Lots of posts elsewhere about this annoying Google G. issue. So at
the
risk of repeating myself, I’ll try sending an email reply to the group
to
see if it goes through or disappears into the ether.
Anyway, first off, thanks for your reply. My particular situation is
that I
have separate tables for individual US states with the two letter state
abbreviation as table prefixes ( e.g. ‘ny_things’, ‘ca_things’). For a
variety of reasons these tables can’t be merged into a single table.
Rather
than define (essentially the same) models for every state, I’d rather
just
have the user select a state, which will the set the state abbreviation
in
the users session data. From then on, all tables needing a prefix will
be
dynamically set according to the session data. Some tables do not need a
prefix at all, so yes, I ran into the same problem trying to set
‘table_name_prefix’ on an individual model and found it was being
prefixed
to all table names.
So for my first stab at a solution, I’m using a before_filter in my
controllers which call a private method in application.rb (so it can be
shared between several controllers) which calls the ‘table_name’ methods
for
those models needing this:
class ApplicationController < ActionController::Base
def set_table_names
Thing.table_name = “#{session[:state_abbreviation]}_things”
end
end
Before this method is called, I call another before_filter method that
makes
sure the the session variable is set and valid. If not, it redirects the
user to select an appropriate value. This is working perfectly for now.
I’m
using a regular expression to ensure that the state abbreviation is in
fact
two and only two lower case letters before it is set in the session, so
I
think I’m fairly safe with this approach.
As far as setting the table name within a model, I’ve always been able
to
just do:
class ExampleModel < ActiveRecord::Base
self.table_name =
“some_crazy_table_name_not_conforming_to_conventions”
end
Cheers,
John-Scott
http://blog.portable-eggplant.org