Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
4 Posts
Observer issue


i have an article model, which when creating a new instance, i am using a virtual attribute technique in order to build the categories associations (categories.build(attrs) for example). the problem is that i have an observer that is supposed to send me an email about any article being created and it's categories, but it seems that in the ActionMailer view, the categories association is empty... so the email is being sent, but with no categories (db shows that they exists!). any idea?

well, i kinda figured out part of the problem, the actual Article#save! triggers first the article instance create, and than the association #create, so the observer triggers when the associations aren't ready yet.

How are you observing the model? Try using after_update or after_save with your observer.

Well, I guess you takes a wrong order for there two steps. Try to build associations in Article#before_create and observe after_create method.

If it doesn't work or not your question at all, please paste part of your code.

4 Posts
Login to add your message