0

So I never know what to do here.

Say you have Order, which has_one Member.

If you call say, my_order.member.first_name where that associated member has been deleted, you can a nilClass error. I can do my_order.member.try(:first_name).. but that just seems like a dumb workaround. I don't want to stick a bunch of tries everywhere.

I've read [this article] on Nil objects: http://robots.thoughtbot.com/post/8181879506/if-you-gaze-into-nil-nil-gazes-also-into-you?

Good stuff. But I would think rails has something handy for this since it's so common.. instead of writing your own custom nilClass or something.

wejrowski
  • 439
  • 5
  • 20
  • [This question](http://stackoverflow.com/questions/7789314/rails-activerecord-how-can-i-make-this-more-concise/7789348#7789348) also addresses this issue. – Dave Newton Oct 20 '11 at 01:45
  • Are we to assume that it's OK if an Order doesn't have an associated Member? – Eric Oct 20 '11 at 03:23

1 Answers1

1

You can use delegate, and you end up with my_order.first_name

DGM
  • 26,629
  • 7
  • 58
  • 79