私は同僚のコードを使っていますが、これが使われているチュートリアルを見つけることができません。誰かがこれが使われているリソースを教えてもらえますか?これはコードをきれいにしましたが、私はそれへの参照が見つかりませんでした。これはこのクラスの一部にすぎません。それは他のいくつかの方法を含んでいます。これは既知のRailsマネージャーのパターンです
class Manager
include ActiveModel::Model
include ActiveModel::Associations
attr_accessor :application_id, :user_id, :user_application_id,.........
belongs_to :application
belongs_to :user_application
belongs_to :user .. more belongs .......
# This method is necessary to enable this ActiveModel Class to be used in views along with Form helpers
def self._reflect_on_association(association) #:nodoc:
_reflections[association.to_sym]
end
def []=(attr, value)
self.send("#{attr}=", value)
end
def [](attr)
multi_attribute_ids = [:some_ids.to_s, :someid2.to_s]
return if multi_attribute_ids.include?(attr)
self.send(attr)
end
def applicant_name
end
-- some more methods
end
このような「マネージャ」の使用はどのようなものでしょうか。ここでself.sendを使用している2つのメソッドは何ですか?これはレールの一般的なパターンですか?
あなたの同僚はどこですか? – MarsAtomic
'def [] =(attr、value)'と 'def [](attr)'は大括弧のセッターとゲッターです。 'manager [:foo]'と 'manager [:foo] = 'bar''を実行すると呼び出されます。 – max
Rubyには( '+'、 '=='、 '<=>'のような)奇妙なメソッドがたくさんあります。 – max