Nubee Callbacks problem


#1

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.

Thanks,

Chris


#2

check out acts_as_versioned plugin/gem


#3

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.

Sounds like a complex thing for rails to achieve

Chris


#4

what about acts_as_list or _tree?


#5

Thanks Alex, you’re a true rails genius.

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().

All of these should preserve validations.

So many choices, so little time :slight_smile:


#6

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().

All of these should preserve validations.

So many choices, so little time :slight_smile:


#7

Chris wrote:

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…


#8

I’m using oracle.

If i set the id before i save it then it is ok.


#9

Chris wrote:

I’m using oracle.

If i set the id before i save it then it is ok.

That might explain it… There’s some adapter-specific code in the
create method that controls how (and if) a new id is generated.


#10

Chris wrote:

So for now i can just manually get the next sequence number.

if you solve it can you tell us how?
Sure. I’m working with MySQL and SQLite, though, not Oracle, so that
may well make a difference.


#11

So for now i can just manually get the next sequence number.

if you solve it can you tell us how?

Thanks,
Chris