2011-08-04 2 views
0

을 감안할 때 다음 모델 나는 대화 모델을 제거하고 메시지에 사용자에 대한 참조를 마이그레이션 할 레일 3 마이그레이션 update_all은/

class User 
    has_many :conversations 
end 

class Conversation 
    belongs_to :user 
    has_many :messages 
end 

class Message 
    belongs_to :conversation 
end 

가입 할 수 있습니다. 는 일반적으로 나는

add_column :messages, :user_id, :integer 

Message.reset_column_information 
Message.all.each do |message| 
    message.user_id = message.conversation.user_id 
end 

remove_column :messages, :conversation_id 

좋아하지만 코드가 업데이트 된 후 실행 생산 마이그레이션에 뭔가를 사용할 수 있습니다. 따라서 오류가 발생합니다.

아마도 약간의 힌트가 필요합니다.

답변

0

좋은 솔루션은 마이그레이션 내부 임시 모델을 정의하는 것입니다 .user_id ->Source: Rails Guides

0

belongs : to relationship 관계를 제거하더라도 메시지에 'conversation_id'가 있어야합니다.

그래서 만약 당신이 한 :

message.user_id = User.find_by_id (message.conversation_id)