Created
June 28, 2011 21:36
-
-
Save fermion/1052306 to your computer and use it in GitHub Desktop.
lol
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 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