2011-11-04 2 views
0

사용 레일레일 3.1 엽기 액티브 동작에서

def create 
@practice = Practice.new(params[:practice]) 

respond_to do |format| 
    if (current_user.practices << @practice rescue false) 

    pmf = current_user.practices_users.inspect # if this line is removed the following update_attributes method breaks! 

    current_user.practices_users.last.update_attributes(:admin_flg => true, :first_name => params[:first_name], :last_name => params[:last_name]) 
    format.html { redirect_to home_dashboard_path, notice: 'Practice was successfully created.' } 
    format.json { render json: @practice, status: :created, location: @practice } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @practice.errors, status: :unprocessable_entity } 
    end 
end 
end 

3.1.0 'PMF = ...'라인이 존재하지 않을 때, 나는이 줄을 얻을 만들기 작업을

NoMethodError: 
    undefined method `update_attributes' for nil:NilClass 

때 'pmf = ...'행이 있으면 작성 조치가 정상적으로 작동합니다. 무슨 일 이니?

+0

'pmf ='줄이 없을 때'current_user.practices_users'의 크기는 얼마입니까? 거래가 완료되지 않았을 수도 있습니다 –

+0

"방법 중단"이라고 말하면 어떻게됩니까? 오류 메시지가 무엇입니까? – CharlieMezak

답변

2

나는 정확한 답을 알고하지 않습니다하지만 난 당신이 디버깅 할 수 있습니다 ...

당신은 레일이에서 (CURRENT_USER에 대한) practices_users의 전체 세트를로드

pmf = current_user.practices_users.inspect 

작업을 수행 할 때 데이터베이스를 호출하고 나중에 last을 호출하면 Array 유형의 객체에 대해 last 메소드가 호출됩니다.

당신은 당신의 PMF 전화, practices_users의 전체 세트가 배열에 데이터베이스에서로드 및되지 않았습니다 만든 데없이

current_user.practices_users.last 

는 레일 모두를하기 때문에 전체 세트를로드하지 않을 수행 할 때 마지막 것이 원한다. 필요한 모든 항목이 마지막 항목 일 때 항목 배열을로드하는 것은 낭비 일 것입니다. 따라서 데이터베이스에서 한 행만로드하는 SQL을 생성합니다.

이러한 상황에서 콘솔 또는 개발 로그를보고 SQLRails가 생성하는 대상과 그 SQL이 반환하는 값을 정확히 확인해야합니다. 그러면 무슨 일이 일어나는지 분명해질 것입니다.

+0

감사합니다. 당신은 저를 시작 시켰고, 문제를 완전히 쫓아 버리기까지 조금 걸릴 것입니다. – mikeborgh