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.
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-16 13: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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-16 13:44
(Received via mailing list)
check out acts_as_versioned plugin/gem
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-16 14: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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-16 14:57
(Received via mailing list)
what about acts_as_list or _tree?
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-16 17: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 :-)
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-16 17: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 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 :-)
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-16 18:13
I'm using oracle.

If i set the id before i save it then it is ok.
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-16 18: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...
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-16 18: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.
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-16 18:31
So for now i can just manually get the next sequence number.

if you solve it can you tell us how?

Thanks,
Chris
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-16 18: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.