How to let Globalize to create all localization after create new object?

250 Views Asked by At

One model, called Tools, of my Rails app is translated with Globalize, when I create a new record, Globalize save into tool_translations table only the record for one locale, but I want save on creation all records in tool_translation, one for each locale.

Now when I create a new Tool the result in tool_translation is this:

+--+--------+-------+------------+---------------------+
|id|tool_id | locale| name       | description         |
+--+--------+-------+------------+---------------------+
|1 | 1      |  en   | Printer    | print documents     |
+--+--------+-------+------------+---------------------+

But I have others locales, so I want this result on tool_translations table after create Tool:

+--+--------+-------+------------+---------------------+
|id|tool_id | locale| name       | description         |
+--+--------+-------+------------+---------------------+
|1 | 1      |  en   | Printer    | print documents     |
+--+--------+-------+------------+---------------------+
|2 | 1      | it    |            |                     |
+--+--------+-------+------------+---------------------+

I have read this: https://github.com/globalize/globalize/issues/620, it's very similar to my problem, but not work for me.

This is my Tool model:

class Tool < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: :slugged
  translates :name, :description
  before_save :name
  def name=(s)
    write_attribute(:name, s.to_s.capitalize)
  end

This is my Tool controller:

def new
    @tool = Tool.new
  end

  def create
    @tool = Tool.new(tool_params)
    if @tool.save
      flash[:success]= t('.created')
      redirect_to admin_tool_path(@tool)
    else
      render 'new'
    end
  end

This method is a way to resolve the problems with Globalize if in the Tool model I will have default_scope -> {order(name: :asc)}

0

There are 0 best solutions below