Attribute changes are not notified and table is not updated
I want to update a table but the update SQL is not performed so the changes has no effect.
Details:
Originally issue.name us 'issue'. I want to change to 'qqqqqqqqqqqq'.
Controller:
def update
if params['cancel']
redirect_to(@issue)
return
end
@issue = Issue.find(params[:id])
logger.debug "original object"
logger.debug "#{@issue.to_yaml}"
logger.debug 'bulk attribute settings...'
@issue.attributes= params[:issue]
logger.debug "after bulk settings"
logger.debug "#{@issue.to_yaml}"
@issue.events.build(:note=>params[:issue][:description],:verb=>'Edited', :changes=>@issue.textalize_changes)
if @issue.save
logger.debug "after save"
logger.debug "#{@issue.to_yaml}"
redirect_to(@issue)
else
render :action => "edit"
end
end
One additional code fregment might be interesting is the textalize_changes
:
def textalize_changes
r = ""
if changed?
changes.keys.each do |k|
r << "#{k.humanize} changed "
r << "from `#{translate(k,changes[k][0])}` "
r << "to `#{translate(k,changes[k][1])}`"
r << "<br/>"
end
end
r开发者_StackOverflow unless r.blank?
end
The result:
- until the line
@issue.save
everything looks correct. I have checked that@issue
contains all the changes i have made. - on the user interface no changes I have notified.
- in the log no update SQL is visible. all relevant select and insert is presented but no update - so no change in table
issues
- even
textalize_changes
has not realize and changes in the object when checking for that (changed?
) - If I manually change the attribute in at source code level by
name=XXX
it is working.
I don't know what to check or review at all. The code is so simple that i have no idea at all.
Here is the log of the operation:
original object
--- !ruby/object:Issue
attributes:
name: OTTO TEST 2
assigned_to: "29"
updated_at: 2010-12-16 10:25:28
project_id: "1"
current_estimate:
lft: "1"
original_estimate:
priority:
id: "10"
version_id:
area_id:
description:
worktype_id: "2"
status_id: "5"
rgt: "2"
parent_id:
created_at: 2010-05-21 07:37:15
fixed_in_version_id:
attributes_cache: {}
bulk attribute settings...
WARNING: Can't mass-assign these protected attributes: description
[4;36;1mIssue Load (0.0ms)[0m [0;1mSELECT "lft", "rgt", "parent_id" FROM "issues" WHERE ("issues"."id" = 10) [0m
after bulk settings
--- !ruby/object:Issue
area:
assigned_user:
attributes:
name: qqqqqqqqqq
assigned_to: "29"
updated_at: 2010-12-16 10:25:28
project_id: "1"
current_estimate:
lft: "1"
original_estimate:
priority:
id: "10"
version_id:
area_id:
description:
worktype_id: "2"
status_id: "5"
rgt: "2"
parent_id:
created_at: 2010-05-21 07:37:15
fixed_in_version_id:
attributes_cache: {}
changed_attributes: {}
children:
events:
fixed_in_version:
iterations:
marked_for_destruction: false
parent:
project:
status:
timelogs:
version:
work_items:
worktype:
[4;35;1mEvent Create (0.0ms)[0m [0mINSERT INTO "events" ("updated_at", "verb", "external", "issue_id", "note", "changes", "user_id", "created_at") VALUES('2010-12-16 10:33:08', 'Edited', 'f', 10, '', NULL, 1, '2010-12-16 10:33:08')[0m
after save
[4;36;1mEvent Load (16.0ms)[0m [0;1mSELECT * FROM "events" WHERE ("events".issue_id = 10) ORDER BY id ASC, created_at ASC[0m
--- &id001 !ruby/object:Issue
area:
assigned_user:
attributes:
name: qqqqqqqqqq
assigned_to: "29"
updated_at: 2010-12-16 10:25:28
project_id: "1"
current_estimate:
lft: "1"
original_estimate:
priority:
id: "10"
version_id:
area_id:
description:
worktype_id: "2"
status_id: "5"
rgt: "2"
parent_id:
created_at: 2010-05-21 07:37:15
fixed_in_version_id:
attributes_cache: {}
Technical info:
- Op system: winXP
- rails: rails 2.3.4
Additional info:
I have a bulk operation with the same purpose which is working correctly. I really don't know the differences:
def update_multiple
if params['cancel']
redirect_to issues_path
return
end
@issues = Issue.find(params[:issue_ids])
@issues.each do |issue|
issue.attributes= params[:issue].reject {|k,v| v.blank? }
issue.apply_template_on_name_change
issue.events.build(:note=>params[:issue][:description],:verb=>"Edited", :changes=>issue.textalize_changes)
issue.save!
end
flash[:notice]="Issues updated!"
redirect_to issues_path
end
Additional info:
If I replace @issue.attributes= params[:issue]
by @issue.attributes= params[:issue].reject {|k,v| v.blank? }
it apply changes and working. But it is not the one I really want. I want to change everything at once. I am going crazy.
Can you try save with exclamation, like @issue.save!
to see the errors.
I have found the issue in awesome nested set.
This plugin reloads the object just before executing nested set operations.: I have changed the controller in the following way:
@issue.parent_issue= params[:issue][:parent_issue]
@issue.attributes= params[:issue].reject {|k,v| 'parent_issue'==k }
this the best solutions now.
note: parent_issue is for setting the parent of the given entity and using the same form as for the rest of the attributes.
精彩评论