ruby on rails - Changing Mongoid class name mid-production -
is possible?
i have mongoid class named magazine
, associations well, re-name publication
. problem have bunch of users have made magazines, issues , articles.
original magazine
model:
class magazine # 1. include mongoid stuff include mongoid::document include mongoid::timestamps include mongoid::slug # 2. define fields field :title, type: string field :description, type: string field :live, type: boolean, default: false field :show_walkthrough, type: boolean, default: true # 3. set attributes accesible attr_accessible :title, :description, :live, :show_walkthrough, :cover_image_attributes, :logo_image_attributes # 4. set slug slug :title # 5. set associations belongs_to :user has_many :issues, dependent: :delete, autosave: true has_one :foreword, :as => :articleable, :class_name => 'article', dependent: :delete, autosave: true embeds_one :cover_image, :as => :imageable, :class_name => 'image', cascade_callbacks: true, autobuild: true embeds_one :logo_image, :as => :imageable, :class_name => 'image', cascade_callbacks: true, autobuild: true # 6. accepting nested attributes accepts_nested_attributes_for :cover_image, :allow_destroy => true accepts_nested_attributes_for :logo_image, :allow_destroy => true # 7. set validations validates_presence_of :title, :description, :cover_image, :logo_image end
i know can change class-name publication
, db.magazines.renamecollection( "publications" )
on mongodb, associations doesn't follow along.
any suggestions?
i looks have association fields in issue , foreword models refer magazine. if happy enough change name of class , underlying collection renaming these association fields main problem. may have like:
class issue belongs_to :magazine end
you redefine association belongs_to :publication
. assuming happy fix references issue#magazine
in code remaining problem issues
collection full of documents have magazine_id
field instead of publication_field
. have 2 options fix database mapping.
first option rename field in database. see mongodb : renaming column name in collection
the second option declare association maps old database field overriding 'foreign key' name:
belongs_to :publication, foreign_key: :magazine_id
you have repeat foreword model , others reference magazine
.
Comments
Post a Comment