gpt4 book ai didi

ruby-on-rails - 从 :accepts_nested_attributes_for 获取关联列表

转载 作者:行者123 更新时间:2023-12-03 15:57:35 25 4
gpt4 key购买 nike

我想得到一个模型的关联列表 :accepts_nested_attributes_for .例如,我想得到 [:children, :other_children]从这个模型:

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

然而,我觉得有一种成熟的方式来获得这个数组。如果是这样,正确的方法是什么。

最佳答案

我不确定这是“正确的方法”,但你不能这样做:

ParentResource.nested_attributes_options.keys

关于ruby-on-rails - 从 :accepts_nested_attributes_for 获取关联列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21466735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com