#140
Dec 15, 2008

Rails 2.2 Extras

This episode finishes up the Rails 2.2 series by showing a few miscellaneous additions that I find useful.
Download (5.7 MB, 4:54)
alternative download for iPod & Apple TV (5.3 MB, 4:54)

Resources

# script/console
Product.find_by_price(3) # => nil
Product.find_by_price!(3) # => RecordNotFound Exception
Product.find_last_by_price(4.99)
Product.all(:joins => :category, :conditions => { :categories => { :name => 'Clothing' } })

# application_helper.rb
def admin_area(&block)
  if admin?
    concat content_tag(:div, capture(&block), :class => 'admin')
  end
end
<% admin_area do %>
  <%= link_to "Edit", edit_product_path(@product) %> |
  <%= link_to "Destroy", @product, :confirm => 'Are you sure?', :method => :delete %>
<% end %>

RSS Feed for Episode Comments -24 comments

1. Swami Atma Dec 15, 2008 at 00:20

Nice tips. You don't need to apologize for anything Ryan. We appreciate every screencast you put out.


2. MTH Dec 15, 2008 at 01:20

Thank you,
hash in conditions is useful as well ;)


3. pulkit Dec 15, 2008 at 01:27

Hey Ryan!
small and sweet screencast!!!
next week I will expect longer screencast ;)

Enjoy the holidays for now.


4. Adam Dec 15, 2008 at 03:51

Time to do a screencast on spam filters for comments.. to save your Recent Comments list :)

cheers


5. Ryan Bates Dec 15, 2008 at 14:37

@Adam, hah, yes. Seems spammers are executing javascript code these days. That should be the only way to post here. I'll try to clean it up soon.


6. Mark Wilden Dec 15, 2008 at 14:57

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".


7. Mike Stramba Dec 16, 2008 at 11:32

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


8. Mike Stramba Dec 16, 2008 at 11:36

Or even better :

clothingProducts = Category.find_by_name('Clothing').products

Mike


9. Kazim Zaidi Dec 16, 2008 at 12:30

@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.


10. PhilT Dec 16, 2008 at 13:18

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.


11. Jim Jones Dec 16, 2008 at 18:40

Great episode. How to handle spam comments would be a great topic too.


12. Hector Parra Dec 18, 2008 at 07:57

@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)


13. lomakin Dec 18, 2008 at 12:42

Did you know, that content_tag is more slover than static tag definition like "<div>#{@hello}</div>"?


14. Lukas Dec 20, 2008 at 08:27

Thanks Ryan, your Railscasts are fantasic!


15. used cars for sale Feb 07, 2009 at 15:44

Oh, Paypal codes!! This blog just loves Paypal and i hate it and wiped its codes off my sites long time ago!


16. Ramon Feb 08, 2009 at 01:08

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


17. Ile malte Mar 04, 2009 at 10:32

Perfect and thx ! (from an ancien php user ...)


18. Vlad Zloteanu Jul 10, 2009 at 01:39

@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.


19. sinema izle Aug 07, 2009 at 15:19

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


20. sinema Dec 15, 2009 at 06:37

thx for the share


21. Venedikta Feb 25, 2010 at 08:59

Thak you very much.


22. Ritanya Mar 09, 2010 at 14:59

Time to do a screencast on spam filters for comments..


23. Gasya Mar 10, 2010 at 14:36

Great episode.


24. Arsentich Mar 11, 2010 at 05:44

Totally agree! Looking forward to next week's full length episode, Ryan. :)


25. Irunya Mar 16, 2010 at 06:21

nice onу !!!


26. gokhan Apr 06, 2010 at 16:38

hmm yes right


27. donmadanfilmizle Jun 17, 2010 at 06:33

thanks for sharing


28. cash for gold Jul 18, 2010 at 18:42

I like your blog,and also like the article,and thank you for provide me so much information :))


29. ed hardyswimwear Jul 20, 2010 at 01:26

fgfdgfdgfdg


30. filmizle24 Jul 25, 2010 at 09:12

nice sharing, thanks for sharing.


31. free directory list Aug 11, 2010 at 22:32

David Heinemeier Hansson


32. jordan air shoes Aug 16, 2010 at 00:34

thank you for provide me so much information. I like your blog,and also like the article


33. Wholesale hats Aug 20, 2010 at 20:51

Good post. I am also going to write a blog post about this...I enjoyed reading your post and I like your take on the issue. Thanks.


34. cool stuff Aug 25, 2010 at 02:14

very cool article ,like my cool stuff .very useful.thanks for sharing the article!


35. can Aug 26, 2010 at 13:54

hmm yes right


36. louis vuitton shoes Aug 26, 2010 at 21:16

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


37. louis vuitton shoes Aug 26, 2010 at 23:22

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


38. Wholesale Electronics Aug 27, 2010 at 00:22

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


39. snow boots Aug 30, 2010 at 21:16

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.


40. GHD Australia Sep 01, 2010 at 01:11

Good choice to come here for more knowledge.


41. GHD Hair Straighteners Sep 01, 2010 at 01:14

It is really an ideal choice to come this website.


42. Cheap Supra Shoes Sep 01, 2010 at 01:16

hope to come here again


43. louis vuitton sunglasses Sep 01, 2010 at 22:32

Nice post. My friend John told me about this blog some weeks ago but this is the first time I’m visting. I’ll undoubtedly be back.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source