-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
I ran into a problem where a
/ belongs_to
relation only works one way. It turned out that when querying the child from the parent, the wrong foreign_key is used. This is what my relations look like:
class Ontology::Job include Mongoid::Document has_many :ontology_synonyms, :class_name => 'Ontology::Synonym' end class Ontology::Synonym include Mongoid::Document belongs_to :ontology_job, :class_name => 'Ontology::Job' end
I expect the foreign key on the child to be
since this is what derives from the relation name, but it queries
job_id
instead.
irb
[36] pry(main)> oj = Ontology::Job.first
MOPED: 127.0.0.1:27017 QUERY database=ontology_development collection=ontology_jobs selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (0.9291ms)
[37] pry(main)> oj.ontology_synonyms
MOPED: 127.0.0.1:27017 QUERY database=ontology_development collection=ontology_synonyms selector=
flags=[] limit=0 skip=0 batch_size=nil fields=nil (53.1111ms)
=> []
The relations metadata contains different foreign_keys depending on the direction queried. I expected them to both be
ontology_job_id
.
irb
[23] pry(main)> Ontology::Synonym.reflect_on_association(:ontology_job).foreign_key
=> "ontology_job_id"
[24] pry(main)> Ontology::Job.reflect_on_association(:ontology_synonyms).foreign_key
=> "job_id"
`