여기 모델 파일의 레일에서 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
에 가치를두고 왜 이해하지만, 다음은 UPDATE
에 NULL
로 설정 모르겠어요. 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
.
해당 업데이트를 수행하는 콜백이 없습니까? –
저는 RoR을 처음 접했습니다. 나는 당신의 의견을 볼 때까지 어떤 콜백이 있었는지 몰랐다. 콜백을 썼다는 말입니까? 전 before_saves가 없습니다. 또한 ProfileTagsController에 업데이트 작업이 없습니다. 죄송합니다. 질문을 이해할 수 없습니다. – user5243421
로그를 보면, 코드는 저장 중에'profile_id'를 NULL로 설정하지 않고'id = options.delete (: id)'라인의 결과 인'id'를 설정합니다. 실제로 일어난 일을 확인하기 위해 데이터베이스를 들여다 보았습니까? 사용자 정의 라인을 포함하도록 로그 항목을 확장 할 수 있습니까? – Jay