I never want to alter a model(row). When a model has been changed i
want a new copy to be created.
What is the best way to implement this?
I think using callbacks might be the answer.
So model.save will create a new row, and leave the old one unchanged.
Any ideas? I can easily implement this in the controller but i’d rather
it be done transparently. Also errors should be passed back to the form
when neccassary.
act_as_versioned copies the old row into a new db, and effectively
replaces the old row with the new row. This is not what i need. The
old row must remain exactly where it is, and a new row created everyime
a row is modified.
I’ve done this like you said. This is your second method:
def before_save
@new_record=true
id,branch_id=nil,nil
end
But it tries to insert the previous primary key into the table,
therefore violating the PK constraint. I have to use a custom primary
key named “branch_id”. Any ideas?
Thanks again,
Chris
Alex Y. wrote:
Chris wrote:
when neccassary.
There are a few ways you could do this. The first would be to override
the new_record? method to always return true, so that when save() gets
called, it always thinks it needs to create() rather than update(). The
second would be to set the value of @new_record to true in a before_save
callback. A third would be to override the save() method to call
create() rather than create_or_update(). A fourth would be to override
create_or_update() to always call create(), and never update().
when neccassary.
There are a few ways you could do this. The first would be to override
the new_record? method to always return true, so that when save() gets
called, it always thinks it needs to create() rather than update(). The
second would be to set the value of @new_record to true in a before_save
callback. A third would be to override the save() method to call
create() rather than create_or_update(). A fourth would be to override
create_or_update() to always call create(), and never update().
I’ve done this like you said. This is your second method:
def before_save
@new_record=true
id,branch_id=nil,nil
end
But it tries to insert the previous primary key into the table,
therefore violating the PK constraint. I have to use a custom primary
key named “branch_id”. Any ideas?
Hmm… Nothing springs to mind. With id set to nil, the create method should get the id from the database. I might give it a look later,
because it’s something I’m going to need myself shortly…