2013-01-22 3 views
0

내에서 관련있는 개체를 결정 : 푸의 인스턴스에 foo_method를 호출 할 때레일 : 많은 관계가있다 - 나는 다음과 같은 관계/방법과 두 개의 클래스가 방법

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 

, 내가 푸 호출되는 말할 수있는 방법을 bar_method 에서요? 이것이 가능한가?

감사합니다.

답변

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 
+0

감사합니다. 나는 자아를 이미 지나치고 있었지만 잔인한 것처럼 보였다. 자동으로 추론 할 방법이 있는지 확인해 보았습니다. 다시 한번 감사드립니다. – user1032752