Skip to content

Instantly share code, notes, and snippets.

@fermion
Created June 28, 2011 21:36
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 fermion/1052306 to your computer and use it in GitHub Desktop.
Save fermion/1052306 to your computer and use it in GitHub Desktop.
lol
class Foo < ActiveRecord::Base
has_one :bar
end
class Bar < ActiveRecord::Base
belongs_to :foo
def something?
# lol
end
end
foo = Foo.new
# I only care about what Foo's Bar#something returns if the associated Bar exists
if foo.bar.try(:something?)
puts "get a haircut"
end
# Alternatively
if foo.bar && foo.bar.something?
puts "or not"
end
# kthx.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment