2016-12-12 3 views
0

첫 번째 컬럼에는 모델에 컬럼을 추가하는 두 개의 마이 그 레이션을 추가했으며 두 번째 마이 그 레이션에서는 최근에 일부 행에 추가 된 컬럼에 값을 저장하는 함수를 실행했습니다 .마이 그 레이션 레일에서 마이 그 레이션 컬럼이 변경되지 않음

문제는 내가 rake db : migrate를 실행할 때 첫 번째 마이그레이션이로드되었지만 데이터베이스가 아직 변경되지 않았기 때문에 두 번째 마이그레이션에서 오류가 발생하므로 한 가지 방법은 두 번 명령을 실행하는 것입니다.

우선 마이그레이션 :

class AddRegisteredReportToSpecialOfferUse < ActiveRecord::Migration 
    def up 
    add_column :special_offer_uses, :registered_report, :boolean, default: false 
    end 

    def down 
    remove_column :special_offer_uses, :registered_report 
    end 
end 

둘째 마이그레이션 :

class CreateReportsFromMigrations < ActiveRecord::Migration 
    def change 
    OneClass.perform 
    end 
end 

OneClass.perform는 속성의 갱신 이전

def perform 
´´´ 
special_offer_uses.update_attribute(:registered_report, true) 
´´´ 
end 

슬로우 오류를 첨가 만드는 방법 :

StandardError: An error has occurred, all later migrations canceled: undefined method `registered_report=

정의되지 않은 메소드는 이전에 추가 된 속성의 이름입니다.

오류가 발생하지 않고 명령을 두 번 실행하지 않는 방법이 있는지 궁금합니다.

는 UPDATE :

I는 reset_column_information에게 다음 요청을 재로드 할 열을 발생 방법을 이용하여 솔루션을 발견했다.

Resets all the cached information about columns, which will cause them to be reloaded on the next request.

The most common usage pattern for this method is probably in a migration, when just after creating a table you want to populate it with some default values

추가 정보 : link

+0

'오류'메시지를 표시 할 수 있습니까? –

+1

난 그 문제가 없었어. 심지어 같은 마이 그 레이션에서 그것을 할 – Fallenhero

+1

2 마이 그 레이션 및 오류 메시지를 넣을 수 – MZaragoza

답변

0

왜 일 이전에 모든 것을 할 수 없습니다?

class AddRegisteredReportToSpecialOfferUse < ActiveRecord::Migration 
    def up 
    add_column :special_offer_uses, :registered_report, :boolean, default: false 
    OneClass.perform 
    end 

    def down 
    remove_column :special_offer_uses, :registered_report 
    end 
end 
+0

가능한 해결책입니다. 그러나 내 문제에 대한 해결책을 찾고 있는데 –

+1

두 번째 마이그레이션을 고집하는 경우'change' 대신'up' 메소드를 사용해보십시오. – luckyruby

관련 문제