【问题标题】:Migrate from belong_to to has_and_belongs_to_many on mongoid without losing data在 mongoid 上从 belong_to 迁移到 has_and_belongs_to_many 而不会丢失数据
【发布时间】:2016-08-04 19:38:19
【问题描述】:

我正在使用带有 mongoid 4.0 的 Rails 4.1 目前,我有一个关系 1-N

class Tenant
  include Mongoid::Document
  belongs_to :tenant, index: true
end

class Experience
  include Mongoid::Document
  has_many :experiences, dependent: :nullify
end

我想将此关系更改为 N-N。为此,我将两种模型都更改为:

class Tenant
  include Mongoid::Document
  has_and_belongs_to_many :tenants, index:true
end

class Experience
  include Mongoid::Document
  has_and_belongs_to_many :tenants, index:true
end

但现在我没有以前的数据了。

【问题讨论】:

    标签: ruby-on-rails mongoid has-and-belongs-to-many migrate mongoid4


    【解决方案1】:

    我最终在控制台上做了类似的事情:

    e = Experience.last
    e.tenant_ids = [e.attributes[:tenant_id]]
    

    所有的经验。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多