不通知属性更改,也不更新表

时间:2010-12-15 11:22:17

标签: ruby-on-rails ruby

我想更新表,但不执行更新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包含我所做的所有更改。
  • 在用户界面上没有我已通知的更改。
  • 在日志中没有更新SQL可见。所有相关的选择和插入都显示但没有更新 - 所以表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: {}

技术信息:

  • 操作系统:winXP
  • rails:rails 2.3.4

其他信息:

我有一个正常工作的批量操作。我真的不知道差异:

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? },则会应用更改并正常工作。但它不是我真正想要的那个。我想立刻改变一切。我疯了。

2 个答案:

答案 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用于设置给定实体的父级,并使用与其余属性相同的表单。