2013-06-07 4 views
0

profile_id 열이있는 사용자 모델과 기본 키 열 ID를 가진 프로필 모델이 있습니다. 프로필 작성 메서드가 실행 된 후에 profile_id 열을 가져 오려면 프로파일 모델의 id 값. after_filter 메서드에서이 작업을 수행 할 수 있다는 것을 알았지 만이 메서드에서 무엇을 작성해야합니까?다른 테이블의 다른 열의 값 가져 오기

+0

당신은 더 잘 질문을 구성해야합니다. 이해할 수 없습니다. –

+0

사용자 모델 profile_id에 열이 있으며 프로필 모드 ID에 열이 있습니다. af 내가 프로필 컨트롤러의 생성 작업을 실행하면 사용자 모델 –

+0

의 해당 profile_id에 프로필 ID를 할당하여 프로필 has_many 사용자를 할당하려고합니까? 사용자가 프로필에 속해 있습니까? –

답변

1

사용자 belongs_to 프로필 및 외래 키 드 사용자 has_one 프로파일이 프로파일의 작성 방법에 프로필 모델

때문에, 프로필 모델 USER_ID했다 필드

에 있어야하는 경우이 뭔가를 할 수 있습니다 이것의

def create 
    @profile = Profile.new(params[:profile]) 
    @profile.user_id = current_user.id 
    respond_to do |format| 
     if @profile.save 
     format.html { redirect_to @profile , notice: 'profile was successfully created.' } 
     else 
     format.html { render action: "new" } 
     end 
    end 
    end 

또는 isntead :

@profile.user_id = current_user.id 

@ profile.user.name @ profile.user.email : 당신이 PARAMS을 통해 ID를 보내는 경우 당신은

@profile.user_id = params[:user_id] 

은 그럼 당신은 이런 관계를 통해 액세스 할 수 있습니다또는 current_user.profile.name

+0

만들기위한 새 after_filter 메서드를 만들고 this_user.update_attribute (: profile_id, @ profile.id)를 추가하고 작업했습니다 나를 위해 –

관련 문제