Answer the question
In order to leave comments, you need to log in
How to display only unique records of the last connection?
There are two models and their relationships:
Model1 -> belongs to -> Model2
Model2 -> has many -> Model1
On the page, I list the elements of the Model2 model.
Everything is fine, but here's how to remove duplicate lines? .uniq
does not help here, since it can only be hung on Model1 in front of.each
Model1.all.each do |item|
item.model2.name # Иногда выводятся одинаковые имена. Надо оставить только одно уникальное имя.
end
Answer the question
In order to leave comments, you need to log in
Wouldn't it be easier to do it the other way around?
Model2.joins(:model1).each do |item|
item.name
end
If the question is about how to make uniq for a collection then
or
If it’s about how to make a selection at all, then it’s better to use the option from fairoj above
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question