Preventing ActiveRecord save() on an instance
There might be a more idiomatic way to do this, but one way would be to set a virtual attribute and check it in a before_save
callback. When you clone the object, set the virtual attribute – maybe something like is_clone
to true
. Then define a before_save
callback for your model class that prevents the save if that attribute is set.
You can use ActiveRecord::Base#readonly!
model = MyModel.find 1
model.readonly!
model.save!
it will raise ActiveRecord::ReadOnlyRecord