2017-02-11 68 views
0

在我的Rails 4應用程序中,我有用戶和帳戶。我想通過關係創建一個has_many。問題是我想要更改類名,以便一個帳戶擁有許多類型用戶的管理員。在另一方面帳戶有許多管理(類型賬戶)多對多與自定義模型名稱在Rails 4中

所以,我有我的過表:

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

在我的帳戶模式,我有:

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

但是當我做Account.first.managers,我得到:

未初始化的經常項目::經理

我忘了什麼?

+0

您可以發佈您的經理類嗎? –

+0

經理類不存在。這是用戶類,但我想在我的關係中稱之爲經理......我不知道這是否變得清晰。 – almo

+0

在這種情況下'AccountManger.first.user'也無法工作。您編寫的'belongs_to'關係將Manager指定爲class_name。你應該寫'belongs_to:manger,class_name:'User',foreign_key :: :(用戶/管理員)_id' –

回答

0

我有它使用自定義名稱工作User,但後來當我爲Account添加自定義名稱,它一直在尋找類型,當它是多態的,應該只能做。我解決不了這個問題,我最終找到這一點,這似乎是完全工作,我相信你需要它(查看網卡的解決方案在底部,沒有公認的答案)的方式:

has_many :through with class_name and foreign_key

基於這個問題的答案,如果你的模型和遷移如下所示,它應該工作:

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 

這裏是一個種子文件來進行測試:

User.create(email: '[email protected]',   first_name: 'Lisa', last_name: 'Hawkins', username: 'lhawkinsa') 
User.create(email: '[email protected]',   first_name: 'Helen', last_name: 'Taylor', username: 'htaylorb') 
User.create(email: '[email protected]',   first_name: 'Gregory', last_name: 'Taylor', username: 'gtaylorc') 
User.create(email: '[email protected]',  first_name: 'Henry', last_name: 'Lane',  username: 'hlaned') 
User.create(email: '[email protected]', first_name: 'Harry', last_name: 'Phillips', username: 'hphillipse') 
User.create(email: '[email protected]',   first_name: 'Jeffrey', last_name: 'Gonzales', username: 'jgonzalesf') 
User.create(email: '[email protected]',   first_name: 'Lori', last_name: 'James', username: 'ljamesg') 
User.create(email: '[email protected]',    first_name: 'Roger', last_name: 'Hill',  username: 'rhillh') 
User.create(email: '[email protected]',  first_name: 'Raymond', last_name: 'Harvey', username: 'rharveyi') 
User.create(email: '[email protected]',    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) 

控制檯輸出:

~/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: "[email protected]", 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: "[email protected]", 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">]>