1


I have the following issue: how to manage a many-to-many association between user and command. I have the following models:

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

Now if i have:

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

1) How can I save their id on the users_commands table?
2) How can I retrieve all the command with user_id =1 later?

I tried with @user.commands.push(@command) but I have an error message:

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

3 回答 3

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
于 2013-09-23T20:23:15.863 回答
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 实例将包含与您的用户的连接。

于 2013-09-23T20:16:04.390 回答
-1

一个问题是

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

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

belongs_to :user
belongs_to :command
于 2013-09-23T20:17:11.830 回答