Data linked by has_many in Rails are duplicated.

Asked 2 years ago, Updated 2 years ago, 50 views

When retrieving data related to the model defined by has_many+through, there was a case where each data was retrieved duplicately, although there was only one data in the database.

For more information, see

Suppose there are tables called "apple" and "orange", and there is an intermediate table called "apple_orange".
Then, put it in Apple's model file

has_many:oranges,:through=>:apple_oranges

If defined as ,

apple.oranges

should be able to retrieve orange data via Apple_orange.

Let's say that the data state is as follows:

 ·Apple

id | title
--------
 1 | AA

·orange

id | title
--------
 1 | AA
 2 | BB
 3 | CC

·Apple_orange

id | apple_id | orange_id
---------------------
 1|       1|        1
 2|       1|        2
 3|       1|        3

In this case, I think Apple.find(1) .oranges has 3 results.

In some cases, the orange data were duplicated and 6 were retrieved.

I have never experienced such a case before, and although it happened once, it has not happened since then.
There is no such thing as two pieces of data at the time of occurrence.
Is it an internal problem with Rails?
Please let me know if you know.

The environment is as follows:

Ruby 1.8.7
Rails 2.3.5
CentOS 5.9

ruby-on-rails ruby

2022-09-30 20:17

1 Answers

This behavior is hard to imagine as an accidental timing bug, but if you have a case, it may remain in Issue of old Rails

Rather, Ruby and Rails are both too old, so I recommend that you upgrade the version quickly.


2022-09-30 20:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.