기존 앱에서 STI를 사용하여 확장 한 기본 모델의 값을 inheritance_column
으로 변경하고 있습니다. 기존 열이 새로운 inheritance_column
과 일치하도록하려면 어떻게 마이그레이션 할 수 있습니까?Rails inheritance_column migration
액티브 :: SubclassNotFound :
class MigrateStoryTypes < ActiveRecord::Migration def self.up Story.all.each { |story| new_story_type = story.story_type.camelize + 'Story' puts "changing #{story.id}'s story_type from #{story.story_type} to #{new_story_type}" story.update_column :story_type, new_story_type } end def self.down Story.all.each { |story| new_story_type = story.story_type.underscore.gsub /_story/, '' puts "changing #{story.id}'s story_type from #{story.story_type} to #{new_story_type}" story.update_column :story_type, new_story_type } end end
그러나, 이것은 실패 :
여기 내 첫 번째 시도이다 'clean_slate'단일 테이블 상속 메커니즘 는 서브 클래스를 찾는 데 실패했습니다. 상속의 경우 'story_type'열이 클래스를 저장하기 위해 예약되어 있기 때문에이 오류는 으로 발생합니다. 상속 클래스를 저장하는 데 을 사용하지 않으려는 경우이 열의 이름을 바꾸거나 해당 정보에 다른 열을 사용하려면 Story.inheritance_column을 덮어 씁니다.
ActiveRecord를 통해이 작업을 수행하는 간단한 방법이 있습니까? 아니면 임시 열, SQL 등을 사용해야합니까?
클래스 이름의 밑줄이 쳐진 버전 (예 :''clean_slate'')이 들어있는'story_type' 컬럼이 이미 있고 이것을 STI로 옮기고,'story_type'을 STI 컬럼으로 사용하고, 'story_type' 값을 클래스 이름에 적용 하시겠습니까? 얼마나 많은'story_type' 값을 가지고 있습니까? –
@muistooshort 바로. 나는 현재'story_type' 값을 두 개 가지고 있습니다. – pdoherty926