简体   繁体   中英

Rails Inner Join not working but the SQL looks right

So I have 2 tables that are joined by an ID. I'm in rails console and I type:

Programmer.all(:joins=>:assignment)

the sql that is generated is:

SELECT `programmers`.* FROM `programmers` INNER JOIN `assignments` ON `assignments`.`programmer_id` = `programmers`.`id`

The output that is generated is the same as Programmer.all. Why doesn't it include the assignments data?

I believe I majorly overanalyzed your question. If you just want to join any available assignments to programmers, you're looking for:

Programmer.all(:include => :assignment)

Rails is designed so that :joins is used to perform things like sorting and grabbing certain records but still keep the query result to a minimum size -- meaning, :joins never actually includes the results from the joined table in the result.

Now here's my previous answer that assumes you want to perform an INNER JOIN to get only the programmers with assignments, but you also want that data. In that case, you have two options:

#1 - Use :select

Programmer.all(:select => '*', :joins => :assignment)

That will change the SQL to:

SELECT * FROM `programmers` INNER JOIN `assignments` ON `assignments`.`programmer_id` = `programmers`.`id`

Upside: You get the query you want and all the data is somewhere, at least.

Downside: assignments is assigned directly to the Programmer object and not to the proper place at Programmer.assignment .

#2 - Use a combination of :joins and :includes

Programmer.all(:joins => :assignment, :include => :assignment)

Which produces the SQL:

SELECT `programmers`.* FROM `programmers` INNER JOIN `assignments` ON `assignments`.`id` = `programmers`.`assignment_id`
SELECT `assignments`.* FROM `assignments` WHERE (`assignments`.`id` IN  (?) )

Upside: All your data is in the right place now. You can refer to programmer.assignment without another query.

Downside: You are running that extra query in a lot of instances. I am fairly sure that Rails tries to optimize this when it needs to, though, and if not, it shouldn't cause you too much overhead.

只需您就可以做到

Programmer.includes(:assignment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM