Get a list of associations from :accepts_nested_attributes_for

ruby-on-rails

Solution

I'm not sure this is the 'correct way', but can't you just do:

ParentResource.nested_attributes_options.keys

Problem

I want to get a list of the associations that a model `:accepts_nested_attributes_for`. For instance, I'd like to get `[:children, :other_children]` from this model: ``` 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.

Original source