Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

471
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda