Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
Bulk creating objects with nested attributes in ruby

We are encountering a problem in a new app we are creating. We have two models: Packages and products, a package has many products. We need to create a controller that can bulk create packages so we started using the activerecord-import gem which allows for bulk creates. However the problem is that the gem does not allow for the use of nested attributes to create products, which means I cannot create the corresponding products for each package. Has anyone encountered a similar problem or have any potential elegant solutions?

The JSON for the controller would be similar as below.

{
  packages: [
    {
      weight: 'x',
      products_attributes: [
        {
          code: x
        },
        {
          code: x
        }
      ]
    },
    {
      weight: 'y',
      products_attributes: [
        {
          code: y
        },
        {
          code: y
        }
      ]
    },
  ]
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

books = 10.times.map do |i|
  book = Book.new(name: "book #{i}")
  book.reviews.build(title: "Excellent")
  book 
end
Book.import books, recursive: true

You can try this, iterate your hash, and build objects for packages, and for each package, iterate products, and build products objects assigned to package object. Another way - is build raw SQL, but it's harder

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda