为什么“save”返回true,但数据库有无效记录?

时间:2012-12-11 12:09:14

标签: sql ruby-on-rails database ruby-on-rails-3

我写了一个Rails应用程序,该社区的前端是json-rpc。 ActiveRecord类是:

class ProcessingDocument < ActiveRecord::Base
  attr_accessible :command, :comment, :creator, :emergency_level, :file_name, :is_locked, :is_removed, :last_status, :next_status, :owner_id, :paper_title, :receiver, :sender, :status, :suggest, :unit_id, :uuid, :workflow_id
  attr_accessor :command, :comment, :creator, :emergency_level, :file_name, :is_locked, :is_removed, :last_status, :next_status, :owner_id, :paper_title, :receiver, :sender, :status, :suggest, :unit_id, :uuid, :workflow_id

  def self.all_propertoes
    [:command, :comment, :creator, :emergency_level, :file_name, :is_locked, :is_removed, :last_status, :next_status, :owner_id, :paper_title, :receiver, :sender, :status, :suggest, :unit_id, :uuid, :workflow_id]
  end
end

Controller动态通过json更新ProcessingDocument的对象。

def pd_create(json)
    pd_now = ProcessingDocument.new(:uuid => generate_uuid)
    json["params"].each do |k, v|   # the json["params"] is a Hash
        if ProcessingDocument.all_propertoes.include? k.to_sym
            pd_now.send("#{k}=", v)
        else
            render json: default_fail_json(json, __method__)
            return
        end
    end

    if pd_now.save
        debug_method pd_now.to_json  # this is a stdout bebug

        result = { :uuid => pd_now.uuid }
        render json: respond_data(json, result, nil)
    else
        render json: default_fail_json(json, __method__)
    end
end

当我发布json {"status":"1", "command":"stuff"}时,debug_method打印:

{"command":"stuff","comment":null,"created_at":"2012-12-11T12:02:41Z",
"creator":null,"emergency_level":null,"file_name":null,"id":16,"is_locked":null,
"is_removed":null,"last_status":null,"next_status":null,"owner_id":null,
"paper_title":null,"receiver":null,"sender":null,"status":"1","suggest":null,
"unit_id":null,"updated_at":"2012-12-11T12:02:41Z",
"uuid":"21403d30-c2c1-4fc8-94ba-36d059fdc170","workflow_id":null}

但数据库不保存“命令”,“状态”和“uuid”:

Started POST "/services/" for 127.0.0.1 at 2012-12-11 20:02:41 +0800
Processing by ServicesController#accept as JSON
Parameters: {"{\"id\":\"7e330302-dede-4d2f-bf52-8e90174bb837\",\"method\":\"pd_create\",\"params\":{\"status\":\"1\",\"command\":\"stuff\"}}"=>nil}
(0.0ms)  begin transaction
SQL (0.5ms)  INSERT INTO "processing_documents" ("command", "comment", "created_at", "creator", "emergency_level", "file_name", "is_locked", "is_removed", "last_status", "next_status", "owner_id", "paper_title", "receiver", "sender", "status", "suggest", "unit_id", "updated_at", "uuid", "workflow_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["command", nil], ["comment", nil], ["created_at", Tue, 11 Dec 2012 12:02:41 UTC +00:00], ["creator", nil], ["emergency_level", nil], ["file_name", nil], ["is_locked", nil], ["is_removed", nil], ["last_status", nil], ["next_status", nil], ["owner_id", nil], ["paper_title", nil], ["receiver", nil], ["sender", nil], ["status", nil], ["suggest", nil], ["unit_id", nil], ["updated_at", Tue, 11 Dec 2012 12:02:41 UTC +00:00], ["uuid", nil], ["workflow_id", nil]]
(2.5ms)  commit transaction
Completed 200 OK in 23ms (Views: 0.2ms | ActiveRecord: 3.5ms)

您已看到SQL:SQL (0.5ms) INSERT INTO "processing_documents" ("command", "comment", "created_at", "creator", "emergency_level", "file_name", "is_locked", "is_removed", "last_status", "next_status", "owner_id", "paper_title", "receiver", "sender", "status", "suggest", "unit_id", "updated_at", "uuid", "workflow_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["command", nil], ["comment", nil], ["created_at", Tue, 11 Dec 2012 12:02:41 UTC +00:00], ["creator", nil], ["emergency_level", nil], ["file_name", nil], ["is_locked", nil], ["is_removed", nil], ["last_status", nil], ["next_status", nil], ["owner_id", nil], ["paper_title", nil], ["receiver", nil], ["sender", nil], ["status", nil], ["suggest", nil], ["unit_id", nil], ["updated_at", Tue, 11 Dec 2012 12:02:41 UTC +00:00], ["uuid", nil], ["workflow_id", nil]]

1 个答案:

答案 0 :(得分:2)

为什么要在模型属性上调用attr_accessor

attr_accessor :command, :comment, :creator, :emergency_level, :file_name, :is_locked, :is_removed, :last_status, :next_status, :owner_id, :paper_title, :receiver, :sender, :status, :suggest, :unit_id, :uuid, :workflow_id

这些是创建设置实例变量的访问器方法,例如attr_accessor :command相当于创建这样的方法:

def command
  @command
end

def self.command=(value)
  @command = value
end

所以现在你的代码中发生的事情是,当你在哈希中的每个键/值上调用pd_now.send("#{k}=", v)时,实例变量被设置而不是数据库属性。这就是为什么在调用save之后没有看到SQL中生成的属性的原因。

要解决此问题,只需删除模型中的attr_accessor行。

参考:Why use Ruby's attr_accessor, attr_reader and attr_writer?