1

I have 2 models: translation_file and translation_group, I upload files in translation_file and the data from those files I want to stock them in translation_group, I'm trying to use the after_create.

in my 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

Does anyone knows a way to do it?

4
  • is there any relationship between TranslationFile and TranslationGroup or do you want to stock the data in a new/existing instance of TransitionGroup? Commented May 9, 2018 at 16:20
  • @sovalina There is no relationship,Yes I want to stock the data in a new instance/existing instance. Commented May 9, 2018 at 16:26
  • If there is no relationship, then why is this a concern of the model? Sounds to me like your architecture would benefit from a layer of abstraction. (I would need to know more context, but for example perhaps this data comes from an input form, and could be processed by a service object rather than an after_create on the model?) Commented May 9, 2018 at 16:33
  • @TomLord You're maybe right, let me give you the context, For the moment i'm trying to upload .yml files, and then proccessing something on them, then trying to show them, that's why I have two tables, and the data I get from the translation_file, I'm trying to stock it in Translation_group after processing some changes, but I'm struggling to do so. Commented May 11, 2018 at 8:21

2 Answers 2

1

You can create/find any TransitionGroup you need inside the method:

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
Sign up to request clarification or add additional context in comments.

Comments

1

With the help of a friend, we created a new Class on /lib that we included in TranslationFile and since I was using Active Storage from Rails 5.2 we had play with the blob.

require 'translation_file_processor'

class TranslationFile < ApplicationRecord
  has_one_attached :file
  after_create :transform_to_translation_groups

  def transform_to_translation_groups
    p = TranslationFileProcessor.new file.blob
    p.process
    true
  end

end

in the lib/translation_file_processor.rb

class TranslationFileProcessor
   include ActiveStorage::Downloading
   attr_reader :blob
   attr_accessor :translations_hash

    def initialize(blob)
      @blob = blob
    end

    def process
      download_blob_to_tempfile do |file|
        ...
      end
      #hash to translation_group
      ...
      group = TranslationGroup.new
      ...
      group.save
    end
end

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.