2013-09-23 110 views
1


我遇到以下問題:如何管理用戶和命令之間的多對多關聯。 我有以下型號:管理Rails中的多對多關聯

class User < ActiveRecord::Base 
has_many :users_commands, dependent: :destroy 
has_many :commands, :through => :users_commands 
end 

class Command < ActiveRecord::Base 
    has_many :users_commands, dependent: :destroy 
    has_many :users, :through => :users_commands 
end 

class UsersCommands < ActiveRecord::Base 
belongs_to :users 
belongs_to :commands 
end 

現在,如果我有:

@user = User.find_by(id: 1) 
@command = Command.find_by(id: 3) 

1)我怎樣才能挽救他們的ID在users_commands表?
2)如何在稍後檢索user_id = 1的所有命令?

我試着用@user.commands.push(@command)但我有一個錯誤信息:

NameError: uninitialized constant User::UsersCommand 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:125:in `compute_type' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/reflection.rb:178:in `klass' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `block in source_reflection' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `collect' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `source_reflection' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/reflection.rb:557:in `check_validity!' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:25:in `initialize' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/associations/has_many_through_association.rb:9:in `initialize' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/associations.rb:157:in `new' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/associations.rb:157:in `association' 
from /var/lib/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/associations/builder/association.rb:70:in `commands' 
from (irb):9 
from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start' 
from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start' 
from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>' 
from bin/rails:4:in `require' 
from bin/rails:4:in `<main>' 
+0

爲節省協會,嘗試@ user.commands.push(@命令)或@ command.users.push(@user)。這將自動創建聯接模型。對於檢索,我假設你已經嘗試@ user.commands和@ command.users?這些方法應該假設您的數據庫設置正確。 – rdmcfee

回答

1

您的型號沒有正確聲明:

class User < ActiveRecord::Base 
    has_many :user_commands, dependent: :destroy 
    #   ^ ^
    has_many :commands, :through => :user_commands 
    #         ^ ^
end 

class Command < ActiveRecord::Base 
    has_many :user_commands, dependent: :destroy 
    #   ^ ^
    has_many :users, :through => :user_commands 
    #        ^ ^
end 

class UserCommand < ActiveRecord::Base 
    # ^ ^
    belongs_to :user 
    #   ^
    belongs_to :command 
    #    ^
end 

然後用這些替代方法之一創建一個新的命令:

@user.commands.create(attributes_hash) 
# or 
command.user_commands.create(user_id: @user.id) 
# or 
command = Command.find(params[:command_id]) 
@user = User.find(params[:id]) 
user_command_relation = UserCommand.create(user_id: @user.id, command_id: command.id) 

一些ActiveRecord的命名約定

  • 型號名稱(類名)應爲單數:User,不Users
  • 的belongs_to的(和HAS_ONE)關係應該是單數:belongs_to :user
  • has_many關係應該是複數:has_many :users
  • 加入模型的n用於許多一對多的關係AME應爲單數:UserCommand
  • 第i個連接表一的has_many關係應被聲明爲複數:has_many :user_commands
0

我會建議你採用這種情況的HABTM關聯。 Rails有一個偉大的核心指南。

http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference

一旦做到這一點,你可以,如果是有直接聯繫和連接記錄會自動創建保存協會。例如

@user.commands.push(@command) 

,或者您可以使用

command = @user.commands.new(attributes) 
command.save 

和所創建的命令實例將包括一個連接到您的用戶。蝙蝠

-1

的一個問題是

class UsersCommands < ActiveRecord::Base 
    belongs_to :users 
    belongs_to :commands 
end 

他們應該被單一化按Rails約定,所以改爲

belongs_to :user 
belongs_to :command