You can rescue all exceptions: %= product.provider. Provider_type. Title rescue nil %.
That would work, but note that it effectively blocks you from ever discovering any bugs. You could type product.totallybogus.would.never. Work rescue nil, and nobody would ever complain (which might be nice, or not :)).
– Casper Jul 24 at 16:23 @Casper, yeah, you are totally right. But it's one of the options. :) – nash Jul 24 at 16:31 rescue nil is working great, Just wanted to know if there if a common way of using this, instead of added it in every line, thanks – sameera207 Jul 25 at 4:24.
This is a common problem. You might want to check these: Object. Try andand Examples: # With try product.provider.
Try(:name) product.provider. Try(:provider_type). Try(:title) # With andand product.provider.andand.name.
To daisy-chain calls and have a graceful failure. +1 – Mike Jul 24 at 17:50 thanks @Casper, I never knew this – sameera207 Jul 25 at 4:25.
Since we can use #try methods or handle the exceptions inline, this method of usage breaks the Law of Demeter: Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. Views should get the ready-to-use ojbects/variables, not chains. So you can try to use delegate method within models and cover other else with helpers.
Keet your views as clean as possible, look at them from designer's point of view.
Good point mikhailov. Sometimes we forget the basics of Good Design with all the blinkelights to play with in the language. – Casper Jul 28 at 22:40.
Default else blockself end end end product.provider. Unless_nil(&:name) product.provider. Unless_nil(&:provider_type).
Unless_nil(&:title) product.provider. Unless_nil("Not specified", &:name) It is also quite convenient to have Object#unless_blank, Array#unless_empty, Hash#unless_empty. Added: Meanwhile using full block is even more convenient in some cases: some_variable = product.provider.
Unless_nil do |provider| # some complex logic here using provider end I like it more than: provider = product. Provider some_variable = if provider # ... end or using product. Provider everywhere.
Matter of taste though.
Rails core has the almost same method #try – mikhailov Jul 25 at 17:11 @mikhailov My project is still on 1.2.6, so I don't have access to try – Victor Moroz Jul 25 at 17:35 this method source is almost the same as yours unless_nil – mikhailov Jul 25 at 17:43 def try(method, *args, &block); send(method, *args, &block); end – mikhailov Jul 25 at 17:44 @mikhailov No, it is not the same (I can check rails source too ;) ). Your method will cause an exception unless you define it in Nil class also by the way. Besides, I don't need args or block in case of association, but I do like scoping the way I showed.As I said it is a matter of taste.
– Victor Moroz Jul 25 at 18:36.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.