Thanks for this one, as optimizing DB queries is always useful.
Thanks for the funny (but true) "comments" in the screencast itself, you just made my day ^^
You can add this 2 lines
to your environment.rb :)
Great screencast as usual !
Though I have one question about your groups show view. How does this work ?
<%= render @group.comments %>
I never saw anything like this and the rails doc about render don't mention it.
Thanks btw !
Thanks for the screencast! I've been wondering when to use either :join or :include. Good stuff. :)
Really great screencast. Haven't really thought about the consequences of include and joins before.
Sorry for asking such a non related question , but how could u show the results in console in columns and showing queries too ? i was running the application and showing results after hitting console b4 , any help would b highly appreciated ....
got a cold Ryan ?
thanks for your useful tips, I really like your "scoped" trick for chaining custom methods.
What is that scoped method? Cant find it in the api
To nest has_many through relationships, try passing the distinct keyword to the select query:
Great episode like always.
Haven't heard about the about the 'scoped' method before. Thanks
Another great screen cast, hope you feel better soon!
Thank you for the code, it's nice and useful
Going back to basics, how did you get your user model into irb? I can do this easily with script/console but if I try User.all i get an uninitialized constant "User" error. Is there a way to have IRB auto load your models like script/console ??
@kiko The very top of the code Ryan posted in the show notes should cause the DB queries to show... I think this is the line you need:
ActiveRecord::Base.logger = Logger.new(STDOUT) # to show logs
Take a look at the link to Hirb in the show notes or at daG's comment to show the DB query results as a table.
Thanks for another great screencast Ryan. You know, I actually look forward to Mondays because of you.
Why is the scoped method not documented?
Ryan, there is an important difference between :include and :joins that you didn't mentioned:
:include does a LEFT JOIN, whereas :joins does an INNER JOIN. Because an INNER JOIN requires matches in both tables, it can NOT be used if the second table has no entries for some records of the first table - because they will be filtered out.
So, IMHO your example "User.all(:joins => :comments, ..." is not a good one. The result will hide users which have no comments at all, so it doesn't result a full user list. In this example you should use :include instead of :joins.
Georg(e)? above is absolutely correct. Thank you by the way Ryan. This was exactly what I was looking for at this exact time, your screencasts are always great! Keep it up!
Again nice screencast!!!
BTW I totally agree with @John.
Please add some captcha like things to stop spamming.
one personal question I want to ask you that have you ever think to change CSS of railscasts.com?
Though it is nice but yet it is same as when it launched.
Joins allows a SQL fragment, so you could write :joins => "LEFT JOIN comments ON ...", according to the Rails documentation.
User.all(:joins => :comments, :select => "users.*, count(comments.id) as comments_count", :group => "users.id")
Ouch, don't do that or your code will not work on Oracle for example, in proper SQL, "group by" expression must contain ALL columns not in computed value (SUM, COUNT, etc..)
I noticed that if you use a select that joins the users table and the comments table, and create a comments_count attribute for accessing the number of comments for each user, there is a problem:
If that user has no comments, they will not be selected. Is there an obvious way around this? I imagine the select method just shouldn't be used if you want to find users who have not commented....
very useful screencast. thanks.
good luck. plz keep it away.
Include vs Joins is very great article ryan
I m glad i found ur blog.Not everyone can provide information with proper flow. Good post. I am going to save the URL and will definitely visit again. Keep it up. thank you!
An interesting article I learned a lot
I was wondering if scoping is possible with eager loading. Without using scope things work fine, but when scoping is introduced new queries get fired.
Problem in detail : here
Great episode thank you Ryan, keep the good work ;-)
First sign in through GitHub to post a comment.