我编写了一个 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]]