RAILS:如何获取模型的has_many关联 [英] RAILS: How to get has_many associations of a model

查看:72
本文介绍了RAILS:如何获取模型的has_many关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取模型的has_many关联?

how I can get the has_many associations of a model?

例如,如果我有此类:

class A < ActiveRecord::Base
  has_many B
  has_many C
end

我会这样:

A.get_has_many

返回

[B,C]

有可能吗?谢谢!

推荐答案

您应该使用ActiveRecord

You should be using ActiveRecord reflections.

然后您可以输入以下内容:

Then you can type something like this:

A.reflect_on_all_associations.map { |assoc| assoc.name}

这将返回您的数组

[:B, :C]

这篇关于RAILS:如何获取模型的has_many关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆