Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

473
Views
undefined method 'keys' when using upsert

I'd like to use the upsert_all method to insert (and update) record from a collection in a simple query, for example:

books = [] 

10.times do |i|
  books << Book.new(name: "book #{i}")
end

Book.upsert_all(books)

however, I get the following error:

.../activemodel-6.1.4.1/lib/active_model/attribute_methods.rb:469:in `method_missing': undefined method `keys' for #<Book id: nil, name: "book 0"> (NoMethodError)

And what I am looking for is to be able to make an insert all or upsert_all of an arbitrary collection as an books for example, if you could help me I would be grateful

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

upsert_all expect an array of hashes, not ActiveRecord objects:

books = [] 

10.times do |i|
  books << { name: "book #{i}" }
end

Book.upsert_all(books)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!