2010-05-28 2 views
1

여기 모델 파일의 레일에서 ID 필드에 NULL두고 :.save는

class ProfileTag < ActiveRecord::Base  
    def self.create_or_update(options = {}) 
    id = options.delete(:id) 
    record = find_by_id(id) || new 
    record.id = id 
    record.attributes = options 
    puts "record.profile_id is" 
    puts record.profile_id 

    record.save! 

    record 
    end 
end 

이 내 로그에 나에게 올바른 인쇄를 제공합니다. 그러나 그것은 또한 profile_id에서 NULL으로 설정하는 UPDATE 호출이 있다고합니다. 다음은 로그 파일에 출력의 일부입니다 :

Processing ProfilesController#update (for 127.0.0.1 at 2010-05-28 18:20:54) [PUT] 
Parameters: {"commit"=>"Save", "profile"=>{"id"=>"2", "password_confirmation"=>"", "username"=>"user2", "first_name"=>"user2_first", "password"=>"", "last_name"=>"user2_last"}, "authenticity_token"=>"...", "tag"=>"1", "id"=>"2"} 
    ?[4;36;1mProfileTag Create (0.0ms)?[0m ?[0;1mINSERT INTO `profile_tags` 
(`reputation_value`, `updated_at`, `tag_id`, `id`, `profile_id`, `created_at`) VALUES(0, '2010-05-29 01:20:54', 1, NULL, 4, '2010-05-29 01:20:54')?[0m 
    ?[4;35;1mSQL (2.0ms)?[0m ?[0mCOMMIT?[0m 
    ?[4;36;1mSQL (0.0ms)?[0m ?[0;1mBEGIN?[0m 
    ?[4;35;1mSQL (0.0ms)?[0m ?[0mCOMMIT?[0m 
    ?[4;36;1mProfileTag Load (0.0ms)?[0m ?[0;1mSELECT * FROM `profile_tags` WHERE (`profile_tags`.profile_id = 4) ?[0m 
    ?[4;35;1mSQL (1.0ms)?[0m ?[0mBEGIN?[0m 
    ?[4;36;1mProfileTag Update (0.0ms)?[0m ?[0;1mUPDATE `profile_tags` SET profile_id = NULL WHERE (profile_id = 4 AND id IN (35)) ?[0m 

나는 내가 INSERT 제대로 profile_id에 가치를두고 왜 이해하지만, 다음은 UPDATENULL로 설정 모르겠어요. ProfileController에서

[편집] : 당신은 더 구체적인 필요하면

def update 
    #...stuff. Set tags array. 
    save_tags(tags) #These tags are correct. Verified by printouts before and after this call. 
    respond_to do |format| 
    if @profile.update_attributes(params[:profile]) 
     flash[:notice] = 'Profile was successfully updated.' 
     #format.html { redirect_to(@profile) } 
     format.html { redirect_to :action=>'show' } 
     format.xml { head :ok } 
    else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @profile.errors, :status => :unprocessable_entity } 
    end 
    end 
end 

def save_tags(tags) 
    profile = find_profile #finds the correct profile. And I confirm that it exists with a printout 
    tags.each do |t| 
    ProfileTags.create_or_update(:profile_id => profile.profile_id, :tag_id => t.id) 
    end 
end 

알려 주시기 바랍니다. save 기능이 데이터베이스에 INSERT 이외의 많은 기능을 수행한다고 생각하지만 올바르게 지정해야 할 부분을 지정해야합니다. profile_id.

+0

해당 업데이트를 수행하는 콜백이 없습니까? –

+0

저는 RoR을 처음 접했습니다. 나는 당신의 의견을 볼 때까지 어떤 콜백이 있었는지 몰랐다. 콜백을 썼다는 말입니까? 전 before_saves가 없습니다. 또한 ProfileTagsController에 업데이트 작업이 없습니다. 죄송합니다. 질문을 이해할 수 없습니다. – user5243421

+0

로그를 보면, 코드는 저장 중에'profile_id'를 NULL로 설정하지 않고'id = options.delete (: id)'라인의 결과 인'id'를 설정합니다. 실제로 일어난 일을 확인하기 위해 데이터베이스를 들여다 보았습니까? 사용자 정의 라인을 포함하도록 로그 항목을 확장 할 수 있습니까? – Jay

답변

2

봐 :

ProfileTags.create_or_update(:profile_id => profile.profile_id, :tag_id => t.id) 

난 당신이 profile.id을 전달하려는 생각, 그리고 (아마도 널)하지 profile.profile_id.

0

할인! 스스로 그렇게해서는 안됩니다.

어쩌면 문제의 이름이 문제 일 수 있습니다. ActiveRecord :: Base에는 이미 save에 의해 호출 된 create_or_update (http://github.com/rails/rails/blob/2-3-stable/activerecord/lib/active_record/base.rb#L2913 참조)라는 메서드가 있습니다! 아마 이걸 대체하면 이상한 문제가 생길거야.

메소드의 이름을 다른 것으로 변경해보십시오. 도움이 될 수 있습니다.

+0

제안 해 주신 Slobodan에게 감사드립니다. 그러나 작동하지 않았습니다. 나는 여전히 profile_id를 NULL로 설정하는 UPDATE를 얻는다. – user5243421

+0

컨트롤러에서 create_or_update를 호출하는 코드를 게시 할 수 있습니까? –

+0

귀하의 요청에 따라 코드가 추가되었습니다! (: – user5243421

0

당신은 당신이 그것을 호출 할 필요가 없습니다, 너무 좋아, 대신 작성 전화, 처음에 create_or_update 방법에 id 속성 전달되지 않습니다 라인에서

def save_tags(tags) 
    profile = find_profile #finds the correct profile. And I confirm that it exists with a printout 
    tags.each do |t| 
    ProfileTag.create(:profile_id => profile.profile_id, :tag_id => t.id) 
    end 
end