2014-01-30 3 views
4

Я хочу получить список ассоциаций, что модель :accepts_nested_attributes_for. Например, я хотел бы получить [:children, :other_children] от этой модели:Получить список ассоциаций от: accepts_nested_attributes_for

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

    accepts_nested_attributes_for :children, :other_children 
end 

Прямо сейчас, я делаю это с помощью следующей функции:

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 

я получаю ощущение, что есть baked- тем самым, чтобы получить этот массив. Если да, то какой правильный метод.

ответ

5

Я не уверен, что это «правильный путь», но вы не можете просто сделать:

ParentResource.nested_attributes_options.keys 
+0

Гораздо более элегантно, чем мое решение. Благодаря! – nullnullnull

Смежные вопросы