代码之家  ›  专栏  ›  技术社区  ›  Shpigford

无法更新附加了关联的属性

  •  0
  • Shpigford  · 技术社区  · 14 年前

    我这里有两个模型: Package Status

    belongs_to 地位

    状态 has_many

    所以,我的包装模型有一个 status_id

    在我的包控制器中,我有这样一个方法(从ajax POST调用接收数据):

    def edit_status
      @status = Status.find_by_name(params[:status])
      Package.update(params[:id], :status_id => @status.id)
    end
    

    状态\u id 为了救我的命。如果我换掉 记录中任何其他列的列(例如 from_name )那就行了。

    我觉得我在吃疯狂的药片,但你知道为什么这不管用吗?

    2 回复  |  直到 14 年前
        1
  •  0
  •   nictrix    14 年前

    在软件包模型中可能需要:

    accepts_nested_attributes_for :status, :allow_destroy => true
    

    http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

        2
  •  0
  •   coder_tim    14 年前

    可能存在验证问题。我会做一个“find”然后“update\u attributes”,它将返回“false”,告诉您记录是否无效。