mardi 27 octobre 2015

rails: has_many_through queries with the wrong id

I'm using rails 3.0.20 and it seems that the has_many with :through relation is not querying with the correct id.

I have ElementType, which is a type of ProjectElement. Each ElementType has a number of ElementTypeParameters. The ProjectElement needs to know about the ElementTypeParameters.

class ProjectElement
  has_many :element_type_parameters, :through => :element_type
  ...
end

The element_type_id of this ProjectElement is 23, and it's id is 14902.

In the console...

>> ProjectElement.find(14902).element_type_parameters
  ElementTypeParameter Load (1.8ms)  SELECT "ELEMENT_TYPE_PARAMETERS".* FROM "ELEMENT_TYPE_PARAMETERS" 
  INNER JOIN "ELEMENT_TYPES" ON ("ELEMENT_TYPE_PARAMETERS"."ELEMENT_TYPE_ID" = "ELEMENT_TYPES"."ID") 
  WHERE (("ELEMENT_TYPES".id = 14902))

Is there any reason why it might be querying the element types table with the project element id instead of it's element_type_id column?

Aucun commentaire:

Enregistrer un commentaire