# Callback's question

**URL:** <https://www.ruby-forum.com/t/callbacks-question/134657>\
**Category:** Rails\
**Created:** [April 13, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/callbacks-question/134657 "2008-04-13T16:25:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ragnarok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@Ragnarok](https://www.ruby-forum.com/u/Ragnarok)\
**Post date:** [April 13, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/callbacks-question/134657/1 "2008-04-13T16:25:02Z")

</div>

## I hava a reply model:

class Reply \< ActiveRecord::Base  
belongs\_to :topic

before\_create { |record|  
Topic.find(record.topic\_id).update\_attributes!(:last\_reply\_id =\>  
record.id)}

## end

## And a topic model:

## class Topic \< ActiveRecord::Base has\_many :replies end

## And i write a topic spec here:

## … it “should know the last reply” do @topic.save reply = @topic.replies.build reply.save reply.topic.should === @topic @topic.last\_reply\_id.should == reply.id end …

last\_reply\_id is for the topic’s latest reply id, when i run the  
test ,i got this :  
‘Topic should know the last reply’ FAILED expected: 10,  
got: nil (using ==)  
spec/models/topic\_spec.rb:26:  
spec/models/topic\_spec.rb:3:

Why i couldn’t save the last\_reply\_id for topic???Help me!!

---

<div class="post-metadata">

**Author:** ![Ragnarok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@Ragnarok](https://www.ruby-forum.com/u/Ragnarok)\
**Post date:** [April 13, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/callbacks-question/134657/2 "2008-04-13T17:02:08Z")

</div>

On Sun, Apr 13, 2008 at 10:24 AM, Ragnarok [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> * * *

Since you’re doing this in before\_create, I don’t think that record (the  
Reply instance) will have an id yet. If you switch the callback to an  
after\_create, does it work?

Craig

---

<div class="post-metadata">

**Author:** ![Ragnarok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@Ragnarok](https://www.ruby-forum.com/u/Ragnarok)\
**Post date:** [April 13, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/callbacks-question/134657/3 "2008-04-13T17:21:32Z")

</div>

On Apr 13, 11:01 pm, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > record.id)}
> 
> > end
> 
> > * * *
> 
> Since you’re doing this in before\_create, I don’t think that record (the  
> Reply instance) will have an id yet. If you switch the callback to an  
> after\_create, does it work?
> 
> Craig

It’s the same too! i try after\_create,before\_save,after\_save …,it  
also couldn’t work!

---

<div class="post-metadata">

**Author:** ![Ragnarok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@Ragnarok](https://www.ruby-forum.com/u/Ragnarok)\
**Post date:** [April 13, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/callbacks-question/134657/4 "2008-04-13T21:59:41Z")

</div>

On Sun, Apr 13, 2008 at 11:20 AM, Ragnarok [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It’s the same too! i try after\_create,before\_save,after\_save …,it  
> also couldn’t work!

I played with this a little bit, but I couldn’t make it work with  
callbacks.  
As an alternative, to get the latest reply’s id, you could just do

@topic.replies.last.id

instead of storing last\_reply\_id on Topic. I think that this approach  
will  
rely on the sequence of IDs instead of record timestamps, but it may  
suffice  
for you.

Craig

---

<div class="post-metadata">

**Author:** ![Ragnarok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@Ragnarok](https://www.ruby-forum.com/u/Ragnarok)\
**Post date:** [April 14, 2008, 4:54am UTC](https://www.ruby-forum.com/t/callbacks-question/134657/5 "2008-04-14T04:54:31Z")

</div>

On Apr 14, 3:58 am, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> instead of storing last\_reply\_id on Topic. I think that this approach will  
> rely on the sequence of IDs instead of record timestamps, but it may suffice  
> for you.
> 
> Craig

Thank you!i try with a virtual attribute, and it’s work !  
but i couldn’t understand , why it doesn’t work with callback method!?  
i want to know the reason!

---

<div class="post-metadata">

**Author:** ![Ragnarok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@Ragnarok](https://www.ruby-forum.com/u/Ragnarok)\
**Post date:** [April 15, 2008, 2:40am UTC](https://www.ruby-forum.com/t/callbacks-question/134657/6 "2008-04-15T02:40:31Z")

</div>

Try reloading your topic model before you check the last\_reply\_id…

Mark

On Sun, Apr 13, 2008 at 10:24 AM, Ragnarok [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :replies  
> it “should know the last reply” do
> 
> Why i couldn’t save the last\_reply\_id for topic???Help me!!
> 
> >

–  
Mark Van H., Partner / Software Developer  
removed\_email\_address@domain.invalid, (616) 706-6842  
Mutually Human Software, [http://mutuallyhuman.com](http://mutuallyhuman.com)
