加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 从以下各项获取关联列表:accepts_nested_attr

发布时间:2020-12-17 02:40:17 所属栏目:百科 来源:网络整理
导读:我想得到一个模型:accepts_nested_attributes_for的关联列表.例如,我想从这个模型中获得[:children,:other_children]: class ParentResource ActiveRecord::Base has_many :children has_many :other_children has_many :non_nested_children accepts_ne
我想得到一个模型: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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读