Sql 为什么";“保存”;返回true,但数据库有无效记录?

Sql 为什么";“保存”;返回true,但数据库有无效记录?,sql,ruby-on-rails,database,ruby-on-rails-3,Sql,Ruby On Rails,Database,Ruby On Rails 3,我已经编写了一个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

我已经编写了一个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
当我发布json
{“status”:“1”,“command”:“stuff”}
时,
debug_方法
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}
但数据库不保存“命令”、“状态”和“uuid”:


您已经看到SQL:
SQL(0.5ms)插入到“处理文档”(“命令”、“评论”、“创建时间”、“创建者”、“紧急级别”、“文件名”、“已锁定”、“已删除”、“上次状态”、“下一个状态”、“所有者id”、“纸张标题”、“接收者”、“发送者”、“状态”、“建议”、“单位id”、“更新时间”、“uuid”、“工作流id”)值中([“接收者”,无;“发送者”,无”,“状态”,无”,“建议”,无”,“单位id”,无”,“更新时间”,周二,2012年12月11日12:02:41 UTC+00:00”,“uuid”,无”,“工作流id”,无]]

为什么在模型属性上调用
attr\u访问器

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\u accessor:command
是否等同于创建如下方法:

def command
  @command
end

def self.command=(value)
  @command = value
end
因此,现在代码中发生的情况是,当您调用
pd_now.send(“#{k}=”,v)
散列中的每个键/值时,正在设置实例变量,而不是数据库属性。这就是为什么在调用
save
后生成的SQL中看不到属性的原因

要解决此问题,只需删除模型中的
attr\u访问器


Ref:

非常感谢!!但是我不明白Ruby的
attr\u accessor
和Rails的
attr\u accessible
之间的区别。这两种方法有很多混淆,事实上它们做的事情非常不同。参见示例:
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 command
  @command
end

def self.command=(value)
  @command = value
end