Harry
Harry

Reputation: 4835

Rails - find_or_create_by giving unexpected results

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.

Upvotes: 1

Views: 856

Answers (1)

Mischa
Mischa

Reputation: 43298

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.

Upvotes: 2

Related Questions