简体   繁体   English

从以下位置获取关联列表:accepts_nested_attributes_for

[英]Get a list of associations from :accepts_nested_attributes_for

I want to get a list of the associations that a model :accepts_nested_attributes_for . 我想得到一个模型:accepts_nested_attributes_for的关联列表。 For instance, I'd like to get [:children, :other_children] from this model: 例如,我想从这个模型中获得[:children, :other_children]

class ParentResource < ActiveRecord::Base
  has_many :children
  has_many :other_children
  has_many :non_nested_children

  accepts_nested_attributes_for :children, :other_children
end

Right now, I'm doing this with the following function: 现在,我正在使用以下函数执行此操作:

def self.nested_associations
  reflect_on_all_associations.map(&:name).select do |association_name|
    association_name if method_defined?("#{association_name}_attributes=".to_sym)
  end
end

I get the feeling that there's a baked-in way to get this array, however. 然而,我觉得有一种烘焙方式来获得这个阵列。 If so, what's the proper method. 如果是这样,那么正确的方法是什么。

我不确定这是'正确的方法',但你不能这样做:

ParentResource.nested_attributes_options.keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM