您如何在Ruby on Rails中为Mongoid建立关系? [英] How do you seed relationships for Mongoid in Ruby on Rails?
问题描述
我正尝试使用Ruby on Rails,Mongo创建一个小型游戏服务器,以Mongoid作为ORM,并使用Devise进行身份验证.我正在尝试修改db/seeds.rb以播种多个用户和游戏文档.
I'm trying to create a small game server using Ruby on Rails, Mongo, with Mongoid as the ORM, with Devise for authentication. I'm trying to modify the db/seeds.rb to seed several users and game documents.
如何在两个Mongo/Mongoid关系之间创建种子?
How do you create a seed between two Mongo/Mongoid relationships?
我有用户和游戏.用户 have_many 游戏.我已经找到了为"embeds_many"和"embedded_in"创建种子数据库的示例,但对于has/所属却不是.如果这是适当的体系结构,则会采取后续措施(将在游戏"中嵌入第三个模型"Turns".
I have Users and Games. Users have_many Games. I've found examples of creating a seed database for "embeds_many" and "embedded_in", but not for has / belongs. A follow-up would be if this is the proper architecture (there's a third model "Turns" that will be embedded in the "Game".
class Game
include Mongoid::Document
belongs_to :user
embeds_many :turns
field :title, type: String
field :user_id, type: Integer
field :current_player, type: Integer
end
class User
include Mongoid::Document
has_many :games
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, :type => String, :default => ""
field :encrypted_password, :type => String, :default => ""
validates_presence_of :email
validates_presence_of :encrypted_password
field :name
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
...
... bunch of fields to support devise gem
结束
我尝试了两种方法来完成这项工作,但都没有在数据库中创建关系:
I've tried two ways to make this work and neither create a relationship in the database:
puts 'EMPTY THE MONGODB DATABASE'
::Mongoid::Sessions.default.drop
puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'First User', :email => 'user@example.com', :password => 'please', :password_confirmation => 'please'
puts 'New user created: ' << user.name
game = Game.create! :title => 'First Game', :user_id => user._id, :current_player => user._id
puts 'New game created: ' << game.title
user.games.push(game)
user.save
game2 = Game.create(:title => 'Foo Game', users: [
User.create(:name => 'd1', :email => 'd1@example.com', :password => 'd', :password_confirmation => 'd'),
User.create(:name => 'd2', :email => 'd2@example.com', :password => 'd', :password_confirmation => 'd'),
User.create(:name => 'd3', :email => 'd3@example.com', :password => 'd', :password_confirmation => 'd')
])
puts 'Second game created: ' << game2.title
推荐答案
您似乎正在手动尝试建立关系.
从游戏模型中删除field :user_id, type: Integer
并尝试
user.games.create!(title: "First Game")
It looks like you are manually trying to create the relationship.
Remove field :user_id, type: Integer
from the Game model
And try
user.games.create!(title: "First Game")
这篇关于您如何在Ruby on Rails中为Mongoid建立关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!