rails 4.2.1 : dynamic table_name

Hi,

I have a problem with dynamic table_name with 4.2.1. With 2.1.9, it was working well

here my simplify models :

class Aaaa < ActiveRecord::Base has_one :bbbb, dependent: :destroy, :inverse_of => :aaaa end

class Bbbb < ActiveRecord::Base belongs_to :aaaa, :inverse_of => :bbbb end

Bbbb.table_name = “cccccs” aa = Aaaa.where(:id => 1)[0] aa.bbbb it runs a select query such as : SELECT “cccccs”.* FROM “cccccs” WHERE “cccccs”.“aaaa_id” = $1 LIMIT 1 [[“aaaa_id”, 1]]

but if then I do

aa = Aaaa.where(:id => 2)[0] Bbbb.table_name = “dddds” aa.bbbb it runs a select query still on table “cccccs” whereas it should be on “dddds” : SELECT “cccccs”.* FROM “cccccs” WHERE “cccccs”.“aaaa_id” = $1 LIMIT 1 [[“aaaa_id”, 1]]

but if I display Bbbb.table_name , it prints “dddds”

when I do

Bbbb.table_name = “cccccs” aa = Aaaa.where(:id => 1)[0] aa.bbbb it runs a select query such as : SELECT “cccccs”.* FROM “cccccs” WHERE “cccccs”.“aaaa_id” = $1 LIMIT 1 [[“aaaa_id”, 1]]

but if then I do

Bbbb.table_name = “dddds” aa = Aaaa.where(:id => 2)[0] aa.bbbb it runs a select query on good table such as : SELECT “dddds”.* FROM “dddds” WHERE “dddds”.“aaaa_id” = $1 LIMIT 1 [[“aaaa_id”, 2]]

once again, with rails 4.1.9, it was working fine.

It seems that with 4.2.1, table for relation are instanciate when the object is create. Is there a reason for that ? Is there a way to avoid that.

No offence, but please no answer as “you shouldnt do this, doing this is not good, or whatever” , I cant also modify my database. I have to do like this, no choice, and for more than 2 years it was working well :wink:

Im aware that it might not supported, even if I dont understand why. Could it be at least possible to know if there is a reason that now the relation is instantiate when the object is created ? I thought one of the good point of rails was to instantiate things only when you need them.

Are polymorphism or STI not supported anymore ? If they are still, why to force us to had a field on database when table_name can depend on domain name for instance.

Between dynamically changing table names have you tried:

Bbbb.reset_column_information

.. before setting the new table name?

thanks for answer I was trying and it didnt change anything.