管理Rails中的多对多关联

时间:2013-09-23 20:03:03

标签: ruby-on-rails associations


我有以下问题:如何管理用户和命令之间的多对多关联。 我有以下型号:

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>'

3 个答案:

答案 0 :(得分: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
  • 用于多对多关系的联合模型名称应该是单数的:UserCommand
  • 连接表的has_many关系应声明为复数:has_many :user_commands

答案 1 :(得分: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

,创建的Command实例将包含您的用户的连接。

答案 2 :(得分:-1)

击球的一个问题是

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

它们应按照Rails约定进行单一化,因此请更改为

belongs_to :user
belongs_to :command