2012-12-11 2 views
0

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 

컨트롤러는 ProcessingDocument의 개체를 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 

내가 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 :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?

+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-accessor-and-attr-accessible –

관련 문제