2014-01-30 23 views
4

我想獲得一個模型:accepts_nested_attributes_for的關聯列表。舉例來說,我想從這個模型得到[:children, :other_children]通過以下方式獲取關聯列表:accep_nested_attributes_for

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-的感覺:

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

    accepts_nested_attributes_for :children, :other_children 
end 

現在,我用下面的函數這樣做在獲得這個數組的方式,但是。如果是這樣,什麼是正確的方法。

回答

5

我不知道這是「正確的方式」,但你不能只是做:

ParentResource.nested_attributes_options.keys 
+0

比我的解決方案更爲靈巧。謝謝! – nullnullnull

相關問題