#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 19 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. David Trasbo Dec 15, 2008 at 01:11

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


3. MTH Dec 15, 2008 at 01:20

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


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


5. Adam Dec 15, 2008 at 03:51

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

cheers


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


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


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


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

Or even better :

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

Mike


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


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


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

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


13. 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)


14. lomakin Dec 18, 2008 at 12:42

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


15. Lukas Dec 20, 2008 at 08:27

Thanks Ryan, your Railscasts are fantasic!


17. 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!


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


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

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


22. aradycom Jun 08, 2009 at 14:01

perfect !!!

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