获取与模型关联的嵌套属性

时间:2013-03-26 09:38:49

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

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

例如

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

是否有现有的辅助方法?如果是,请告诉我。 :)

由于

1 个答案:

答案 0 :(得分: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]