Skip to content

HABTM associated model neglects to return all fields from the join table. #23

@amfriedman

Description

@amfriedman

When I use Containable with a normal find() with a HABTM relationship (e.g., Contest HABTM Candidate), it pulls all fields in the join table (e.g., CandidateToContest) that links to the two HABTM primary models. This is expected behavior.

But when I use EagerLoader and run the same query, the join table (CandidateToContest) only returns the two linked foreign key fields (e.g., contest_id and candidate_id). It neglects to return any other field from this join table, even its own primary key (id)!

I see that the problem is this query generated by EagerLoader. If the user does not specify fields in the HABTM definition or the find() call, then you might want to simply remove these in the fields portion of this query and replace with CandidateToContest.* -- just like the other tables involved:

SELECT `Candidate`.*, `Candidate`.`id`, 
`CandidateToContest`.`candidate_id`, 
`CandidateToContest`.`contest_id`, 
(`CandidateToContest`.`contest_id`) AS `EagerLoaderModel__assoc_id` 
FROM `mydatabase`.`candidates` AS `Candidate` 
INNER JOIN `mydatabase`.`candidates_contests` AS `CandidateToContest` 
ON (`Candidate`.`id` = `CandidateToContest`.`candidate_id`) 
WHERE `CandidateToContest`.`contest_id` = (69) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions