0

json-rpcによってフロントエンドとコミュニティ化するRailsアプリを作成しました。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

そして、コントローラーは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}

ただし、データベースは「command」、「status」、および「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]]

4

1 に答える 1

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)、ハッシュ内の各キー/値を呼び出すときに、データベース属性ではなくインスタンス変数が設定されているということです。そのため、を呼び出した後に生成されたSQLに属性が表示されませんsave

これを修正するにattr_accessorは、モデルの線を削除するだけです。

参照:なぜRubyのattr_accessor、attr_reader、attr_writerを使用するのですか?

于 2012-12-11T12:39:20.127 に答える