On Thu, 16 Dec 2004, Roeland Moors wrote:
> I''m trying to make a website with rails. This website will have 2
> different environments A en B.
> The tables in environment A will have prefix ''A_'' en
''B_'' in B.
> There is also an extra table ''users'' that needs to be
accessible
> in the two environments. Is that possible?
>
> I''m new to rails and I''m just in the thinking phase so
there is a
> risk that this is a stupid question.
Looks like nobody''s answered you yet, so I will, having looked at this
code just recently. (But at the 0.8.5 version, not 0.9.)
Table prefix is a global setting. So you can''t use standard Rails
facilities to do this.
However, though the magic of object-oriented programming, it''s not hard
to do what you want. There''s just a little trick, because Rails wants
to use your inheritance hierarchy for its own purposes, and you have to
know the magic phrase to put a stop to this.
You want to subclass ActiveRecord::Base:
class PrefixedActiveRecord < ActiveRecord::Base
# Turn off single-table-inheritance.
# (This is the "trick" to allow you to use subclasses.)
def self.descends_from_active_record?; true end
def table_name_prefix
# Code to hand out the appropriate prefix at the appropriate time.
end
end
Now make your ActiveRecord objects inherit from PrefixedActiveRecord,
and those instances will ignore the global @@table_name_prefix attribute
and instead do what you programmed it to do.
Just watch it carefully, though, since changes to ActiveRecord could
make the trick no longer work. But by reading through the code you
should be able to find the new one, if it changes.
cjs
--
Curt Sampson <cjs-gHs2Wiolu3leoWH0uzbU5w@public.gmane.org> +81 90 7737
2974 http://www.NetBSD.org
Make up enjoying your city life...produced by BIC CAMERA