Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Ivan Dimitrov
Ivan Dimitrov
10,587 Points

delete statuses of destroyed user on treebook application

Cancel account on Rails App(treebook) is crashing the app, because I think when canceling the account, his statuses are still posted and they have no user_id. The first error is

ActionView::Template::Error (undefined method `gravatar_url' for nil:NilClass):

I am guessing that when a user cancels his account, his statuses should be removed from the statuses index page and that's what is causing the error.

Can anyone help with that?

Thanks! :)

1 Answer

Brandon Barrette
Brandon Barrette
20,485 Points

You need to add :dependent => :destroy to the has_many :statuses

has_many :statuses, :dependent => :destroy

Then, when the user is deleted, so are their statuses. There are some other options, like nullifying and such. See: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Deleting+from+associations

Ivan Dimitrov
Ivan Dimitrov
10,587 Points

Thanks a lot :) I worked nice!

I also added :dependent => :destroy to :friends and :user_friendships so they can get deleted too.