我收到此错误:
NameError in GamesController#create
uninitialized constant User::UsersGame
app/controllers/games_controller.rb:43:in `create'
这令人困惑,因为我不知道它为什么引用UsersGame而不是UserGame ......
我已经尝试重命名事物,采用新方法(仍然困惑我为什么需要新的和创造,但我想我知道他们都需要在那里),并且搞乱我的迁移以确保我拥有正确的表格,但我不能让它工作。用户类通过设计自行工作,所以我不认为这可能是一个问题。无论如何,这里是相关文件。
模型中的users_games.rb作为连接表
class UserGame < ActiveRecord::Base
belongs_to :user
belongs_to :game
end
模型中的user.rb
class User < ActiveRecord::Base
has_many :users_games
has_many :games, :through => :users_games
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
模特中的game.rb
class Game < ActiveRecord::Base
has_many :users_games
has_many :users, :through => :users_games
# has_many :turns, :dependent => :destroy
attr_accessible :name, :creator
end
答案 0 :(得分:1)
实际上,您需要使用has_and_belongs_to_many
代替has_many through
。
has_and_belongs_to_many :games , join_table: 'users_games'
如果您将联接表更改为games_users,则只需使用:
has_and_belongs_to_many :games
请按照:
有关habtm的更多信息(避免重复输入):
Rails nested form on HABTM: how to prevent duplicate entry?
希望有所帮助