Get a list of associations from: accepts_nested_attributes_for

I want to get a list of associations that have a model :accepts_nested_attributes_for. For example, I would 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

Now I am 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 is a baked way to get this array. If so, what is the correct method.

+4
source share
1 answer

I am not sure if this is the “right way”, but you cannot just do:

ParentResource.nested_attributes_options.keys
+5
source

Source: https://habr.com/ru/post/1524597/


All Articles