Forum: Ruby on Rails Nubee Callbacks problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris (Guest)
on 2006-03-16 14:37
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
Chris H. (Guest)
on 2006-03-16 14:44
(Received via mailing list)
check out acts_as_versioned plugin/gem
Chris (Guest)
on 2006-03-16 15:45
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
Chris H. (Guest)
on 2006-03-16 15:57
(Received via mailing list)
what about acts_as_list or _tree?
Alex Y. (Guest)
on 2006-03-16 18:06
(Received via mailing list)
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 :-)
Chris (Guest)
on 2006-03-16 18:51
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 :-)
Chris (Guest)
on 2006-03-16 19:13
I'm using oracle.

If i set the id before i save it then it is ok.
Alex Y. (Guest)
on 2006-03-16 19:24
(Received via mailing list)
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 Y. (Guest)
on 2006-03-16 19:28
(Received via mailing list)
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.
Chris (Guest)
on 2006-03-16 19:31
So for now i can just manually get the next sequence number.

if you solve it can you tell us how?

Thanks,
Chris
Alex Y. (Guest)
on 2006-03-16 19:52
(Received via mailing list)
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.
This topic is locked and can not be replied to.