class Foo < ActiveRecord::Base has_one :bar end class Bar < ActiveRecord::Base end
で、
foo.bar.name
とアクセスしたい。が、bar が nil の場合に怒られてしまう(当たり前)。
いちいちif文で回避するしかないのかなぁ?Null Objectパターンみたいなの使えないんだろうか。あるいはもっとエレガントな回避策。
うーむ。
class Foo < ActiveRecord::Base has_one :bar end class Bar < ActiveRecord::Base end
で、
foo.bar.name
とアクセスしたい。が、bar が nil の場合に怒られてしまう(当たり前)。
いちいちif文で回避するしかないのかなぁ?Null Objectパターンみたいなの使えないんだろうか。あるいはもっとエレガントな回避策。
うーむ。