I need to save a copy of an ActiveRecord object so I'm using something like:
@original = Model.find(params[:id])
@copy = @original.clone
However, when I attempt to save this to Postgres:
PGError: ERROR: null value in column "id" violates not-null constraint
In the console:
@copy.id
=> nil
How do I get ActiveRecord to not send the id column in the INSERT statement?
Update:
The problem went away after I made a small change:
@new = Model.new(params[:model])
@original = Model.find(x)
@clone = @original.clone
@new.foreign_key_id = @clone.foreign_key_id
I replaced the last line with:
@new.foreign_key_id = @original.foreign_key_id
This change removed 'id' from the INSERT statement. After a successful experiment with this change, I immediately reverted the code and attempted to reproduce the error. I have been unable to reproduce the error. Friday the 13th, maybe?