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 -- Posted via http://www.ruby-forum.com/.
check out acts_as_versioned plugin/gem On 3/16/06, Chris <evilgeenius@gmail.com> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/71a7042f/attachment.html
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 -- Posted via http://www.ruby-forum.com/.
what about acts_as_list or _tree? On 3/16/06, Chris <evilgeenius@gmail.com> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/75eb267e/attachment.html
Chris wrote:> 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.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 :-) -- Alex
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 Young 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 :-)-- Posted via http://www.ruby-forum.com/.
I''m using oracle. If i set the id before i save it then it is ok. -- Posted via http://www.ruby-forum.com/.
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... -- Alex
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. -- Alex
So for now i can just manually get the next sequence number. if you solve it can you tell us how? Thanks, Chris -- Posted via http://www.ruby-forum.com/.
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. -- Alex