Callback's question

I hava a reply model:

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!

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

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!

Try reloading your topic model before you check the last_reply_id…

Mark