Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails associations in namespaced models

I'm re-doing an app and migrating data from an old app. The some of the models names will be the same, though not all of them.

I'm writing a rake task to connect to the old database, read the records, do some stuff and write the result into a new database. Because some of the table names are the same the model names will be the same, so I want to name space my models like so

module OldData
    class Account <ActiveRecord::Base
      has_many :subcriptions
      establish_connection $olddb  
    end

    class Subscription <ActiveRecord::Base
      belongs_to :account
      establish_connection $olddb  
    end
end

where $olddb is a hash required to connect to the old database

I can open account records and read them ok, but the Account model doesn't have a subscriptions association. The latest Rails documentation suggest that this should work. but it doesn't.

Any advice?

like image 433
John Small Avatar asked Jun 01 '13 14:06

John Small


1 Answers

maybe you should try to set class name explicitly

has_many :subcriptions, class_name: 'OldData::Subscription'

and

belongs_to :account, class_name: 'OldData::Account' 
like image 54
Rostyslav Diachok Avatar answered Nov 14 '22 22:11

Rostyslav Diachok