I have an entity which is an ActiveRecord. When I call entity.save it returns true, and the entity.new_record? returns false, but the entity.id is 0. In the entities table, the new row was still inserted with the correct incremented id. It’s very weird.
My rails version was 3.2.8, database is Mysql, and use gem mysql2 (0.3.11). I’ve struggled with this for a whole day…
Code added: json is a string from frontend.
o = JSON.parse(CGI.unescape(json))
if o.has_key?('cls') then
@r = o['cls']
cls = o['cls']
#create
if o['action'] == 0 then
entity = Object.const_get(cls).new
o.each_pair do |k,v|
# entity.respond_to? check method exist
if defined? entity[k] then
entity[k] = v
end
end
r = entity.save!
and the model (generated by rails g model):
class Node < ActiveRecord::Base
attr_accessible :iNodeId, :level, :name, :pNodeId, :stationId, :type
end
Works by changing
to this