nifty rails 3.0 methods
Daniel Willemse di 22 jun 10
So with the Rails 3.0 beta release as of late, I decided to try and play around with a new rails app. I’m pretty new to Ruby and Rails, so trying a new version of Rails (where they also changed routing, one of the things I personally have most problems with) was pretty challenging for me.
For those that haven’t yet checked out the new and cool stuff that’s available with rails 3, here’s some new code versus the 2.x version.
I had a fresh app in rails 2.3.5, which I tried to rebuild in rails 3.0.
In the old app for instance, there was code like:
Invoice.find(:all, :conditions => ["number > ?", 5]) or Invoice.find(:all, :order => "number DESC")
While in the new and improved rails,
you can write it much easier like this:
Invoice.where("number > ?", 5) and Invoice.order("number DESC")
The cool part about this, is that you can chain these new methods like this:
Invoice.order("number DESC").where("number > ?" 5)
This technique also works for named scopes. With rails 3.0, you can easily make a named scope as follows:
scope :above_1, where("number > 1") scope :below_5, where("number < 5") scope :above_1_and_below_5, above_1.below_5
(notice how it’s no longer called named_scope!!)
These scopes don’t make any sense, but you get the picture.
There are more of these new methods and as I play more with rails 3.0 I’ll try and update
So besides making it easier for you to chain together find options, why would you use these above the find methods
in rails 2.x?
Well I found my answer in a railscast where it is explained that the find option has been revised.
http://railscasts.com/episodes/202-active-record-queries-in-rails-3
In the video, it is shown that for instance, Model.scope (Article.recent in the video) returns an active record relation instead of performing the query. And that the query is performed when you try and get the actual data.
I have not yet been able to reproduce the same queries as in the railscast so I’ve not figured out how it works.
When I do however, I’ll try and share my findings and post an update.
Daniel Willemse
(the code I use, which should show me an active record relation in the console)
class Invoice < ActiveRecord::Base scope :above_1, where("number > 1") scope :below_5, where("number < 5") scope :above_1_and_below_5, above_1.below_5 end
>> i = Invoice.above_2_and_below_5.all
returns:
=>#<Invoice id: 4, …. ….. ….. , updated_at: "2010-06-22 19:50:29">]
Where I expect it to return something along the lines of
#<ActiveRecord::NamedScope::Scope
Gepost in hor | 1 reactie
Welkom op Holland On Rails
Het startpunt voor Ruby On Rails in Nederland. Vind de laatste technieken, meningen en nieuwtjes.Recente Jobs
Gezocht: Ruby On Rails ontwikkelaar (junior of senior)
Eet, drink en droom jij over Ruby On Rails? Wil jij het liefste dag en nacht bezig zijn met jehobby; super coole webapplicaties ontwikkelen in Ruby On Rails?
Dan willen wij jou graag een podium bieden om je Ruby skills te vertonen aan onze nationale en internationale klanten!
@ Internetbureau Holder, Obdam
Bekijk alle jobs »»
Gereedschapskist
Onmisbare tools vooriedere developer!
- Ruby On Rails
Framework voor de web 2.0 developer. Eindelijk vooruitgang! - TextMate
Editor for true pro's
Typ, tab, top :-)
Nee, niet voor Win. - Made On A Mac
En nou is het over met die saaie grijze Windows bak van je!
Auteurs op deze site
Chris Obdam
'Less is more' evangelist, past dit ook dagelijks toe op zijn tandenborstel.Chiel Wester
Snelheidswonder op Ruby wielen. Leuk om mee te pair-programmen ;-)
Wijnand Wiersma vr 25 jun 10 00:08
Hi Daniel,
I see you call it with .all and that is a query triggerer.
If you leave that off it will return an active relation object.
Plaats je reactie