Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8311429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:48:47+00:00 2026-06-08T19:48:47+00:00

I am currently seeding my database, and the section below is giving me some

  • 0

I am currently seeding my database, and the section below is giving me some results I did not expect:


trim.model_years.find_or_create_by_value([
 {value: '2009 (58)'},
 {value: '2009 (09)'},
 {value: '2009 (59)'},
 {value: '2010 (59)'},
 {value: '2010 (10)'},
 {value: '2010 (60)'},
 {value: '2011 (60)'},
 {value: '2011 (11)'},
 {value: '2011 (61)'},
])

I would have expected this to treat each of these individually, and do a find_or_create for the record (I adapted the formatting from a normal create method, where it would simply create 9 separate records).

What seems to be happening, however, is that I am getting 1 record, which is a string containing all of the records, including “value:” etc.

How can I format this to allow 9 separate records to be find_or_created?

EDIT: Many thanks for the answer about iterating over the array – I now seem to be getting somewhere, but still having an issue, which is that instead of creating one record and additional associations to it, the seed file is still creating duplicate records in each loop. I have included my modified code below:


make = Make.find_or_create_by_value(value: 'Alfa Romeo')
model = make.models.find_or_create_by_value(value: '147')
trim = model.trims.find_or_create_by_value(value: '2.0 Lusso 5d (2001 - 2005)')
values = [
 {value: '2001 (X)'},
 {value: '2001 (Y)'},
 {value: '2001 (51)'},
 {value: '2002 (51)'},
 {value: '2002 (02)'},
 {value: '2002 (52)'},
 {value: '2003 (52)'},
 {value: '2003 (03)'},
 {value: '2003 (53)'},
 {value: '2004 (53)'},
 {value: '2004 (04)'},
 {value: '2004 (54)'},
 {value: '2005 (54)'},
 {value: '2005 (05)'}
]
values.each do |item|
  trim.model_years.where(item).first_or_create
end

There are, of course, many blocks like this, all of which should be re-using the same model years, but instead first_or_create seems to be always creating duplicates.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:48:48+00:00Added an answer on June 8, 2026 at 7:48 pm

    By iterating over your array:

    values = [
      {value: '2009 (58)'},
      {value: '2009 (09)'},
      {value: '2009 (59)'},
      {value: '2010 (59)'},
      {value: '2010 (10)'},
      {value: '2010 (60)'},
      {value: '2011 (60)'},
      {value: '2011 (11)'},
      {value: '2011 (61)'},
    ]
    
    values.each do |item|
      trim.model_years.find_or_create_by_value(item[:value])
    end
    

    By the way, Rails Guides recommends using first_or_create instead of find_or_create_by. With first_or_create your code could look like this:

    values.each do |item|
      trim.model_years.where(item).first_or_create
    end
    

    Sorry, but I don’t understand your new question. This shouldn’t create duplicate records. It should create the model years for each model. Something like this:

    id | model_id | value
    =========================
     1 |        1 | 2001 (X)
     2 |        1 | 2001 (Y)
     3 |        1 | 2001 (51)
     4 |        1 | 2002 (51)
     5 |        1 | 2002 (52)
     6 |        2 | 2001 (X)
     7 |        2 | 2001 (Y)
     8 |        2 | 2001 (51)
    

    So, yes, there will be duplicate values, but all the row will be unique. If this is not what you want, you have to remodel your database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Case Currently I'm working on database seeding script, executed with sqlcmd . For example
We are currently performing searches on a Database and returning results in JSON format
I'm currently trying to pick a database vendor. I'm just seeking some personal opinions
I am trying to get Linq2SQL to work with my legacy database. I currently
I currently have a large database for GPS Tracking purposes. What happens is each
I have a MySQL database (currently using phpmyadmin to manage) that I would like
I'm currently creating a lot of classes that will access database using a connection
HI all! My client currently has a SQL Server database that performs 3-4 Million
I'm interested in learning some (ideally) database agnostic ways of selecting the n th
Following the last section in the GCM: Getting Started guide, there's some book-keeping to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.