我正在尝试编写一个测试用例,我正在寻找一种更简洁的方法来获取与模型关联的嵌套属性
例如
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
有没有现成的辅助方法?如果是,请告诉我。 :)
谢谢
最佳答案
根据 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]
关于ruby-on-rails - 获取与模型关联的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15633683/