1


次の問題があります: ユーザーとコマンドの間の多対多の関連付けを管理する方法。私は次のモデルを持っています:

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

バットからの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 に答える