2017-02-03 3 views
0

条件付きロジックに基づいて複数の他のオブジェクトの1つに関数または一連の関数を委譲できますか?条件付きでRubyでアクティブなレコードを委譲する方法

例:この場合

class Item 
    has_one :thing 
    has_one :other_thing 

    delegate :foo, 
      :bar 
      to: :[thing or other_thing] 
end 

、私は事に委任、またはいくつかの条件付きロジックに基づいて他の事することのいずれかにアクションfooとbarをしたいと思います。

おそらく関連:Active Record with Delegate and conditions

答えて

1

はい。

他のオブジェクトに加えて、関数を他の関数に委譲することもできます。

例:

class Item 
    has_one :thing 
    has_one :other_thing 

    delegate :foo, 
     :bar 
     to: :correct_thing 

    def correct_thing 
     [conditional] ? thing : other_thing 
    end 
end 
0

以下のような何か:

class Item 
has_one :thing 
has_one :other_thing 

delegate :foo, 
     :bar 
     to: :thing if: thing_condition? 

delegate :foo, 
     :bar 
     to: :otherthing if: otherthing_condition? 

エンド

関連する問題