Load the model created by an "after_create" around filter

1 view
Skip to first unread message

Elvis Fernandes

unread,
Nov 10, 2009, 5:11:48 AM11/10/09
to restfulx-...@googlegroups.com
Hi!

I'm using an around filter in my model, something like this:

class Task < ActiveRecord::Base
  after_create :update_reminder
  
  has_one :reminder
  
  def update_reminder
    reminder = Reminder.new
     # set its properties
     reminder.save
  end
end

On the Flex side, when I create a task (using task.create() ), the related reminder is properly created on the database, but it is not cached by RestfulX, even if I use "include" in my controller:

format.fxml  { render :fxml => @task, :include => :reminder }

Which is the proper way to load into the cache the model created by  the "after_create" method above?

Thanks!

Elvis Fernandes

Dima Berastau

unread,
Nov 10, 2009, 4:53:35 PM11/10/09
to restfulx-...@googlegroups.com
Hi Elvis, not sure about that syntax, try that stuff out on ./script/console and/or verify by calling directly from your browser to make sure you are getting correct results, I'd say the following should work:

render :fxml => @task.to_fxml(:include => :reminder)

Dima

Elvis Fernandes

unread,
Nov 13, 2009, 9:17:34 AM11/13/09
to restfulx-...@googlegroups.com
Hi, Dima!

The syntax I used really doesn't work. Thank you for the tip.

Now I'm using @task.to_fxml(:include => :reminder) but it still doesn't work (on Flex). If I try a GET on the browser, the reminder is shown with the task, but my problem is into the create method. This is what the log says (I truncated the larger lines):

Processing TasksController#create to fxml ...
4;36;1mSQL (0.0ms) [0m   [0;1mBEGIN [0m
4;35;1mTask Create (15.0ms) [0m   [0mINSERT INTO `tasks` ...
4;36;1mReminder Load (0.0ms) [0m   [0;1mSELECT * FROM `reminders` WHERE (`reminders`.task_id = 20) ...
4;35;1mReminder Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `reminders` [0m
4;36;1mReminder Create (0.0ms) [0m   [0;1mINSERT INTO `reminders`...
4;35;1mSQL (94.0ms) [0m   [0mCOMMIT [0m

I suppose that the task.reminder is being loaded at the third line, but it is being really created at the fifth line! I think this is why the reminder isn't being loaded into RestfulX's cache.

I also tried do not use the after_create filter and call the update_reminder method manually, someting like this (into tasks controller, create method):

if @task.save
    task.update_reminder

In the log, the difference is that all sql commands to create the task and load the reminder are wrapped into a BEGIN ... COMMIT block and the sql command to create the reminder (fifth line posted above) is wrapped into another BEGIN...COMMIT block.

I have no clue on how to solve this. I think I'll need some workaround into the Flex side, something like reloading my models (reminders).

Some suggestion?

Thank you again!

Elvis Fernandes

Elvis Fernandes

unread,
Nov 13, 2009, 1:42:38 PM11/13/09
to restfulx-...@googlegroups.com
Hi, Dima!

Just to give a feedback: I'm using a workaround, by calling the task.reload() into the onSuccess method after task.create(), and including the to_fxml(:include => :reminder) in my controller ...

I think this is not the better solution ... if someone have another idea ...

Thank you!

Elvis Fernandes
Reply all
Reply to author
Forward
0 new messages