2012-12-11 86 views
0

我已经写了一个Rails应用程序,社区的前端由json-rpc。 ActiveRecord类是:为什么“保存”返回true,但数据库有无效记录?

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]]

回答

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行。

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

+1

非常感谢!但我无法理解Ruby的'attr_accessor'和Rails的'attr_accessible'之间的区别。 – Ghjhdf

+0

关于这两种方法存在很多混淆,它们实际上做了非常不同的事情。见例如:http://stackoverflow.com/questions/4700785/using-attr-accessor-and-attr-accessible-on-the-same-field http://stackoverflow.com/questions/3136420/difference-between- ATTR访问器和ATTR可访问 –

相关问题