Rails的:有很多的关系 - 确定哪些相关的对象从方法

问题描述:

内我有两个班,以下关系/方法:Rails的:有很多的关系 - 确定哪些相关的对象从方法

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_method,我怎么能知道哪些美孚称为这从bar_method内?这可能吗?

谢谢

是的!这是一个简单的方法;)

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