John Small
John Small

Reputation: 973

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?

Upvotes: 17

Views: 10996

Answers (1)

Rostyslav Diachok
Rostyslav Diachok

Reputation: 811

maybe you should try to set class name explicitly

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

and

belongs_to :account, class_name: 'OldData::Account' 

Upvotes: 36

Related Questions