Created
September 6, 2012 12:28
-
-
Save KensoDev/3655751 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NilUser | |
def display_name | |
"Uknown user" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ActiveRecord::Base | |
belongs_to: user, nil_class: NilUser | |
end |
class Post < ActiveRecord::Base
belongs_to :user
def author
user.try(:name)
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
imo you've just overlooked the purpose of the null object pattern and/or the availability of #try in rails.