我想更新表,但不执行更新SQL,因此更改无效。
详细说明:
最初是issue.name我们'问题'。我想改成'qqqqqqqqqqqq'。
控制器:
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
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 unless r.blank?
end
结果:
@issue.save
一切看起来都正确。我已检查@issue
包含我所做的所有更改。issues
textalize_changes
在检查时没有实现和更改对象(changed?
)name=XXX
的属性,则表明它正在运行。我根本不知道要检查或检查什么。代码非常简单,根本不知道。
以下是操作的日志:
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: {}
技术信息:
其他信息:
我有一个正常工作的批量操作。我真的不知道差异:
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
其他信息:
如果我将@issue.attributes= params[:issue]
替换为@issue.attributes= params[:issue].reject {|k,v| v.blank? }
,则会应用更改并正常工作。但它不是我真正想要的那个。我想立刻改变一切。我疯了。
答案 0 :(得分:0)
您可以尝试使用感叹号进行保存,例如@issue.save!
以查看错误。
答案 1 :(得分:0)
我在令人敬畏的嵌套集中发现了这个问题。
此插件在执行嵌套集操作之前重新加载对象:我已按以下方式更改控制器:
@issue.parent_issue= params[:issue][:parent_issue]
@issue.attributes= params[:issue].reject {|k,v| 'parent_issue'==k }
这是现在最好的解决方案。
注意:parent_issue用于设置给定实体的父级,并使用与其余属性相同的表单。