0

In my Rails 4 app I have users and accounts. I want to create a has_many through relationship. Th thing is that I want to change the class name so that one account has many managers of the type user. The account on the other hand has many Managed (of type account)

So I have my through table:

class AccountManager < ActiveRecord::Base
  belongs_to :account, class_name: 'Managed'
  belongs_to :user, class_name: 'Manager'
end

In my account model I have:

has_many :account_managers
has_many :managers, :through => :account_managers, :source => :user

But when I do Account.first.managers, I get:

uninitialized constant Account::Manager

Anything I forgot?

almo
  • 6,107
  • 6
  • 43
  • 86
  • Can you post your Manager class? – Abhishek Kumar Feb 11 '17 at 15:13
  • Manager class does not exists. It's the User class but I want to call it Manager in my relationship....I don't know if that becomes clear. – almo Feb 11 '17 at 20:54
  • In that case `AccountManger.first.user` wouldn't work either. The `belongs_to` relation you have written specifies Manager as a class_name. You should be writing `belongs_to :mangers, class_name: 'User',foreign_key: :(user/manager)_id` – Abhishek Kumar Feb 12 '17 at 07:10

1 Answers1

0

I had it working with a custom name for User, but then when I added a custom name for Account, it kept looking for type, which it should only do when it is polymorphic. I couldn't resolve that issue and I ended up finding this, which appears to be working fully the way I believe you need it to (look at Nic's solution at the bottom, not the accepted answer):

has_many :through with class_name and foreign_key

Based on that answer, if your models and migrations look as follows, it should work:

class Account < ActiveRecord::Base
  has_many :manager_account_pairs
  has_many :account_managers, through: :manager_account_pairs, source: :user
end

class User < ActiveRecord::Base
  has_many :manager_account_pairs
  has_many :managed_accounts, through: :manager_account_pairs, source: :account
end

class ManagerAccountPair < ActiveRecord::Base
  belongs_to :account
  belongs_to :user
end

class CreateAccount < ActiveRecord::Migration
  def change
    create_table :accounts do |t|
      t.string :name, index: { unique: true }
      t.timestamps null: false
    end
  end
end

class CreateUser < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email, index: { unique: true }
      t.string :first_name
      t.string :last_name
      t.string :username, index: { unique: true }
      t.timestamps null: false
    end
  end
end

class CreateManagerAccountPair < ActiveRecord::Migration
  def change
    create_table :manager_account_pairs do |t|
      t.integer :account_id
      t.integer :user_id
      t.timestamps null: false
    end
  end
end

Here is a seed file to test with:

User.create(email: 'lhawkinsa@icio.us',            first_name: 'Lisa',    last_name: 'Hawkins',  username: 'lhawkinsa')
User.create(email: 'htaylorb@imdb.com',            first_name: 'Helen',   last_name: 'Taylor',   username: 'htaylorb')
User.create(email: 'gtaylorc@unblog.fr',           first_name: 'Gregory', last_name: 'Taylor',   username: 'gtaylorc')
User.create(email: 'hlaned@whitehouse.gov',        first_name: 'Henry',   last_name: 'Lane',     username: 'hlaned')
User.create(email: 'hphillipse@howstuffworks.com', first_name: 'Harry',   last_name: 'Phillips', username: 'hphillipse')
User.create(email: 'jgonzalesf@com.com',           first_name: 'Jeffrey', last_name: 'Gonzales', username: 'jgonzalesf')
User.create(email: 'ljamesg@sfgate.com',           first_name: 'Lori',    last_name: 'James',    username: 'ljamesg')
User.create(email: 'rhillh@gnu.org',               first_name: 'Roger',   last_name: 'Hill',     username: 'rhillh')
User.create(email: 'rharveyi@tripadvisor.com',     first_name: 'Raymond', last_name: 'Harvey',   username: 'rharveyi')
User.create(email: 'sperryj@mit.edu',              first_name: 'Stephen', last_name: 'Perry',    username: 'sperryj')

Account.create(name: 'Ooba')
Account.create(name: 'Avamba')
Account.create(name: 'Linktype')
Account.create(name: 'Brainsphere')
Account.create(name: 'Wordtune')

ManagerAccountPair.create(account_id: 1, user_id: 1)
ManagerAccountPair.create(account_id: 2, user_id: 2)
ManagerAccountPair.create(account_id: 3, user_id: 3)
ManagerAccountPair.create(account_id: 4, user_id: 4)
ManagerAccountPair.create(account_id: 5, user_id: 5)
ManagerAccountPair.create(account_id: 1, user_id: 6)
ManagerAccountPair.create(account_id: 2, user_id: 7)
ManagerAccountPair.create(account_id: 3, user_id: 8)
ManagerAccountPair.create(account_id: 4, user_id: 9)
ManagerAccountPair.create(account_id: 5, user_id: 10)

Console output:

~/workspace/rails_four_example>> rails c
Running via Spring preloader in process 8465
Loading development environment (Rails 4.2.7.1)

    2.3.3 :001 > Account.first.account_managers
Account Load (1.2ms)  SELECT  "accounts".* FROM "accounts"  ORDER BY "accounts"."id" ASC LIMIT 1
User Load (0.8ms)  SELECT "users".* FROM "users" INNER JOIN "manager_account_pairs" ON "users"."id" = "manager_account_pairs"."user_id" WHERE "manager_account_pairs"."account_id" = $1  [["account_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<User id: 1, email: "lhawkinsa@icio.us", first_name: "Lisa", last_name: "Hawkins", username: "lhawkinsa", created_at: "2017-02-12 19:58:42", updated_at: "2017-02-12 19:58:42">, #<User id: 6, email: "jgonzalesf@com.com", first_name: "Jeffrey", last_name: "Gonzales", username: "jgonzalesf", created_at: "2017-02-12 19:58:42", updated_at: "2017-02-12 19:58:42">]> 

    2.3.3 :002 > User.first.managed_accounts
User Load (0.8ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
Account Load (0.5ms)  SELECT "accounts".* FROM "accounts" INNER JOIN "manager_account_pairs" ON "accounts"."id" = "manager_account_pairs"."account_id" WHERE "manager_account_pairs"."user_id" = $1  [["user_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Account id: 1, name: "Ooba", created_at: "2017-02-12 19:58:42", updated_at: "2017-02-12 19:58:42">]> 
Community
  • 1
  • 1
eggroll
  • 1,049
  • 9
  • 18