Ruby on rails “我怎样才能搬家?”;“你有很多”;从一个集合到另一个集合的关联和字段?

Ruby on rails “我怎样才能搬家?”;“你有很多”;从一个集合到另一个集合的关联和字段?,ruby-on-rails,migration,mongoid,has-many,Ruby On Rails,Migration,Mongoid,Has Many,我对罗很陌生。抱歉,如果我使用了错误的术语,或者答案很明显 最初,我有一个用户模型,如下所示 class User include Mongoid::Document devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :to

我对罗很陌生。抱歉,如果我使用了错误的术语,或者答案很明显

最初,我有一个用户模型,如下所示

class User
  include Mongoid::Document

  devise :database_authenticatable,
         :registerable,
         :recoverable,
         :rememberable,
         :trackable,
         :validatable,
         :token_authenticatable,
         :omniauthable

  has_many :foos
  field :name, :type => String
  # Some other company fields
  ....
end

class Foo
  include Mongoid::Document

  belongs_to :user
  ...
end
此初始用户模型用于表示公司

然后,我决定添加另一个模型,该模型将具有与初始用户模型不同的角色,因此我开始使用多态关联,并将必要的字段从用户模型移动到公司模型。我还添加了一个与公司没有直接关系的经理模型。我基本上使用用户模型进行设计

class User
  include Mongoid::Document

  devise :database_authenticatable,
         :registerable,
         :recoverable,
         :rememberable,
         :trackable,
         :validatable,
         :token_authenticatable,
         :omniauthable

  belongs_to :rolable, :polymorphic => true
end

class Company
  include Mongoid::Document

  has_one :user, :as => :rolable

  has_many :foos
  field :name, :type => String
  # Some other company fields
  ....
end

class Manager
  include Mongoid::Document

  has_one :user, :as => rolable
end

class Foo
  include Mongoid::Document

  belongs_to :company
  ...
end

到目前为止,对于新用户注册来说,一切似乎都很顺利。但是,我必须转换旧数据库。让我困惑的是,我以前有过很多联想。我已经实现了迁移(使用这个gem)来将字段从用户模型移动到公司模型,但我仍然不知道如何处理关联。

如果不需要将信息从旧数据库传输到新数据库,则不需要运行迁移


MongoDB可以在文档上有一些无用的密钥,这并没有问题。唯一的问题是在数据库中保存额外的八位字节。

我最后编写了一个迁移,将必要的字段移动到新模型。我还在想是否还有其他可能的解决办法。