I an application where a Post belongs_to :user
I want to retain posts for deleted users. This can cause errors in the view when viewing a post whose author was deleted. I tried to do this:
class Post < ActiveRecord::Base
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
def author
author || NullUser.new
super
end
end
This causes a 'stack level to deep` error. Why? I could do this:
class Post < ActiveRecord::Base
belongs_to :user
def author
user || NullUser.new
end
def author=(user)
self.user = user
end
end
But it doesn't seem right to mess with my associations this way. What's the best way to go about this?