Relational databases and the SQL language used to access them have no concept of "last" without an ordering. Just in case it's not clear (as it wasn't clear to me) in the Rails world, :last means "with the highest id".
@Mike
I think you missed the idea.
The extra nested hash in conditions helps you specify WHERE conditions on the associations you've joined.
I can't remember a good example right now, but quite often you need to join two or more associations, where such an addition to Rails serves to beautify code.
@Mike
Your answer may seem easier but you missed that with your sentences you end up making 2 queries while it was originally only one.
By the way, I'd like to understand it too, PhilT! You can give a try to a really nice pluggin about writing "rubyish" queries: Squirrel (http://thoughtbot.com/projects/squirrel)
Excellent, as always. I took your tutorial (from episode 40 as well) and made it more customizable. I wanted to share it with everyone (note this is not heavily tested!):
See my blog post:
http://railshacks.blogspot.com/2009/02/blocks-in-view.html
or dive straight into the code:
http://pastie.org/383089
In Ryan's example, there is only one query executing in the background. In your example, you are doing 2 queries. On more heavy joins/queries, it will matter that you can do only one query and let the DB handle the optimisation.
Nice tips. You don't need to apologize for anything Ryan. We appreciate every screencast you put out.
Thank you,
hash in conditions is useful as well ;)
Hey Ryan!
small and sweet screencast!!!
next week I will expect longer screencast ;)
Enjoy the holidays for now.
Relational databases and the SQL language used to access them have no concept of "last" without an ordering. Just in case it's not clear (as it wasn't clear to me) in the Rails world, :last means "with the highest id".
I guess if you work with Rails everyday all day that :
>
Product.all(:joins => :category, :conditions => {:categories => {:name => 'Clothing'}})
makes sense, and is "easy" to remember ? ;)
This seems a lot easier to me :
clothing=Category.find_by_name("Clothing")
prods=Clothing.products
I thought that was one of the purpoes for ActiveRecord, i.e. to eliminate having to remember cryptic syntax ?
What does the first syntax do that the second one doesn't?
Mike
Or even better :
clothingProducts = Category.find_by_name('Clothing').products
Mike
@Mike
I think you missed the idea.
The extra nested hash in conditions helps you specify WHERE conditions on the associations you've joined.
I can't remember a good example right now, but quite often you need to join two or more associations, where such an addition to Rails serves to beautify code.
Like this!
:select => 'DISTINCT tracks.*',
:joins => [{:song => :stores}, {:song => :genres}],
:conditions => {:instrument_id => instrument_ids, :stores => {:id => id}, :genres => {:id => genre_ids}}
Needed to make a simple search feature work.
What I don't understand is why this works and yet :include doesn't. If you try to use includes you get outer joins but it also ignores the select.
@Mike
Your answer may seem easier but you missed that with your sentences you end up making 2 queries while it was originally only one.
By the way, I'd like to understand it too, PhilT! You can give a try to a really nice pluggin about writing "rubyish" queries: Squirrel (http://thoughtbot.com/projects/squirrel)
Did you know, that content_tag is more slover than static tag definition like "<div>#{@hello}</div>"?
Thanks Ryan, your Railscasts are fantasic!
Excellent, as always. I took your tutorial (from episode 40 as well) and made it more customizable. I wanted to share it with everyone (note this is not heavily tested!):
See my blog post:
http://railshacks.blogspot.com/2009/02/blocks-in-view.html
or dive straight into the code:
http://pastie.org/383089
@Mike,
In Ryan's example, there is only one query executing in the background. In your example, you are doing 2 queries. On more heavy joins/queries, it will matter that you can do only one query and let the DB handle the optimisation.
This episode has been updated to Rails 5 as a blog post Rails 5 Extras