1

我正在尝试编写一个测试用例,我正在寻找一种更简洁的方法来获取与模型关联的嵌套属性

例如

class Category < ActiveRecord::Base

accepts_nested_attributes_for :devices, :books

#other association and code is not written here

end

我预计

$ Category.get_nested_attributes #this is a dummy method
$ ["devices","books"] #this is the return i expect

是否有任何已经存在的辅助方法?如果是,请告诉我。:)

谢谢

4

1 回答 1

5

根据 rails 源代码(https://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L269),此代码应该可以工作:

1.9.3p327 :002 > User.nested_attributes_options.keys
 => [:subscriptions] 
于 2013-03-26T09:50:28.480 回答