开发者

Getting a list of related models in rails

开发者 https://www.devze.com 2023-01-15 12:04 出处:网络
Suppose I have an object Person开发者_JS百科, which has_many :foos and :bars. Given an instance, p (p = Person.new), how do I programmatically determine what relationships are available?

Suppose I have an object Person开发者_JS百科, which has_many :foos and :bars.

Given an instance, p (p = Person.new), how do I programmatically determine what relationships are available?

i.e. p.some_method => ["foo", "bar"]


You can use Active Record Reflections (API here)

In your example:

p.class.reflect_on_all_associations(:has_many).collect {|a| a.name}
0

精彩评论

暂无评论...
验证码 换一张
取 消