从另一个模型创建数据

时间:2018-05-09 16:00:10

标签: ruby-on-rails ruby

我有2个模型:translation_file和translation_group,我在translation_file中上传文件,我想在translation_group中存储这些文件中的数据,我正在尝试使用after_create。

在我的models / translation_file.rb中:

class TranslationFile < ApplicationRecord
  has_many_attached :yaml_files
  after_create :file_to_data

  def file_to_data
    hash = YAML.load_file()#our file here
    t = load_translation(hash)
    #hash to translation_group
  end
end

有谁知道这样做的方法?

1 个答案:

答案 0 :(得分:1)

您可以在方法中创建/查找所需的任何TransitionGroup

def file_to_data
  hash = YAML.load_file()#our file here
  t = load_translation(hash)
  group = TransitionGroup.find_by( #any attribute you want)
  #or TransitionGroup.new

  group.attribute_for_storing_data = t
  group.save
end
相关问题