I have written a Rails app that communities with front end by json-rpc. The ActiveRecord Class is:
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
And the Controller dynamic updates the object of ProcessingDocument by json.
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
When I post a json {"status":"1", "command":"stuff"}, the debug_method print:
{"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}
But the database don’t save the “command”, ,”status” and “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)
You have see the 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]]
Why do you call
attr_accessoron your model attributes?These are creating accessor methods which set instance variables, e.g.
attr_accessor :commanddoes the equivalent of creating a method like this:So what is happening in your code right now is that when you call
pd_now.send("#{k}=", v)on each key/value in the hash, instance variables are being set rather than database attributes. That is why you are not seeing the attributes in the SQL generated after you callsave.To fix this, just remove the
attr_accessorline in your model.Ref: Why use Ruby's attr_accessor, attr_reader and attr_writer?