Skip to content

Instantly share code, notes, and snippets.

@KensoDev
Created September 6, 2012 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KensoDev/3655751 to your computer and use it in GitHub Desktop.
Save KensoDev/3655751 to your computer and use it in GitHub Desktop.
class NilUser
def display_name
"Uknown user"
end
end
class Post < ActiveRecord::Base
belongs_to: user, nil_class: NilUser
end
@krupenik
Copy link

krupenik commented Sep 9, 2012

imo you've just overlooked the purpose of the null object pattern and/or the availability of #try in rails.

@sobrinho
Copy link

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