I’ve decided to make a major change in my application which affects
more than 1 model. I don’t want to wipe all the data though. Here’s
the scenario I’m in:
I have a Book model with a Chapter model. Book has many Chapters and
Chapter belongs to Book. However, I want to add a Story model between
them so that Book has many Stories and Stories has many Chapters. Is
there a way I can make this change without wiping all my data?
Find or create the matching story. (Find params will depend on
structure of story and chapter.)
story.book = chapter.book
chapter.story = story
chapter.book = nil
Remove column chapters.book_id.
Could also write a one-off script to do this if you’re more comfortable,
but
leveraging ActiveRecord in this case will make life easier no matter
where
you decide to implement it.
HTH.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.