Wenn ich versuche, verknüpfte Daten in Index-Aktion zu zeigen - ich kann nicht vermeiden, N + 1 Problem mit Standart umfasst. Beispiel:Rails - Mehr über "N + 1"
Model 1 - Pet (let it will be animal: title:string. Pet can has many PetAttributeElems, they show attributes this Pet have and what values they have)
Model 2 - PetAttribute (this model contains only titles of attributes, like weight, age and 1000+ more attributes. PetAttribute has many PetAttributeElems - one attribute, such as weight, can be described for many Pets)
Model 3 - PetAttributeElem (this model belongs to pet and to petAttribute, also it has value field, that show value of attribute for pet.
Als ich show
Aktion machen - ich in HAML:
[email protected]_attribute_elems.includes(:pet_attribute).each do |elem|
="#{elem.pet_attribtute.title}: #{elem.value}"
Aber wenn ich index
Aktion machen möchte ich nutzen, um:
[email protected] do |pet|
-pet.pet_attribute_elems.includes(:pet_attribute).each do |elem|
="#{elem.pet_attribtute.title}: #{elem.value}"
Das includes
Methode nennen viele SQL-Abfragen, für jede pet
Jetzt löse ich es manuell durch zusätzliche Objekt wie folgt erstellen:
@pet_elems = {}
PetAtributeElems.includes(:pet_attribute)
.where(pet_id:@pets.map(&:id)).each do |elem|
pet_id = elem.pet_id
if @pet_elems.include?(pet_id)
@pet_elems[pet_id] << elem
else
@pet_elems[pet_id] = [elem]
end
end
als ich verwenden kann:
[email protected] do |pet|
-if @pet_elems.include?(pet.id)
[email protected]_elems[pet.id].each do |elem|
="#{elem.pet_attribtute.title}: #{elem.value}"
Wie konnte ich diese Aufgabe leichter lösen?
[Sie vielleicht‘ die falsche Aufgabe lösen (semi-related reading) ...] (https://stackoverflow.com/questions/26247288/reducing-n1-queres-using-the-bullet-and-r Spec-Gems/26251892 # 26251892) –
Ich hoffe, Sie haben Recht) aber jetzt muss ich im Index Aktion verknüpfte Daten ohne N + 1 Abfragen zeigen – user2572790