开发者

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`
开发者_StackOverflow中文版

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.


Simply you can do like

Programmer.includes(:assignment)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜