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

477
Vistas
Rails - How to reference model's own column value during update statement?

Is it possible to achieve something like this?

  • Suppose name and plural_name are fields of Animal's table.
  • Suppose pluralise_animal is a helper function which takes a string and returns its plural literal.
  • I cannot loop over the animal records for technical reasons.
  • This is just an example
Animal.update_all("plural_name = ?", pluralise_animal("I WANT THE ANIMAL NAME HERE, the `name` column's value"))

I want something similar to how you can use functions in MySQL while modifying column values. Is this out-of-scope or possible?

UPDATE animals SET plural_name = CONCAT(name, 's') -- just an example to explain what I mean by referencing a column. I'm aware of the problems in this example.

Thanks in advance

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I cannot loop over the animal records for technical reasons.

Sorry, this cannot be done with this restriction.

If your pluralizing helper function is implemented in the client, then you have to fetch data values back to the client, pluralize them, and then post them back to the database.

If you want the UPDATE to run against a set of rows without fetching data values back to the client, then you must implement the pluralization logic in an SQL expression, or a stored function or something.

UPDATE statements run in the database engine. They cannot call functions in the client.

over 4 years ago · Santiago Trujillo Denunciar

0

Use a ruby script to generate a SQL script that INSERTS the plural values into a temp table

File.open(filename, 'w') do |file|
  file.puts "CREATE TEMPORARY TABLE pluralised_animals(id INT, plural varchar(50));"
  file.puts "INSERT INTO pluralised_animals(id, plural) VALUES"
  Animal.each.do |animal|
    file.puts( "( #{animal.id}, #{pluralise_animal(animal.name)}),"
  end
end

Note: replace the trailing comma(,) with a semicolon (;)

Then run the generated SQL script in the database to populate the temp table.

Finally run a SQL update statement in the database that joins the temp table to the main table...

UPDATE animals a
INNER JOIN pluralised_animals pa 
  ON a.id = pa.id
SET a.plural_name = pa.plural;
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