0

次の関係/メソッドを持つ2つのクラスがあります。

class Bar
  has_many :foos

  def bar_method
    #puts the specific foo that called it
  end
end

class Foo
  belongs_to :bar

  def foo_method
    bar.bar_method
  end
end

Foo のインスタンスで foo_method を呼び出す場合、bar_method 内からどの Foo がこれを呼び出したかをどのように判断できますか? これは可能ですか?

ありがとう

4

1 に答える 1

1

はい、そうです!これが簡単な方法です;)

class Bar
  has_many :foos

  def bar_method foo
    puts foo
  end
end

class Foo
  belongs_to :bar

  def foo_method
    bar.bar_method self
  end
end
于 2013-01-22T01:39:02.163 に答える