#2
Mar 06, 2007

Dynamic find_by Methods

Shorten simple finds considerably and improve readability by using the dynamic find_all_by and find_by methods.
Download (4.8 MB, 1:28)
alternative download for iPod & Apple TV (2.5 MB, 1:28)
# tasks_controller.rb
def incomplete
  @tasks = Task.find_all_by_complete(false)
end

def last_incomplete
  @task = Task.find_by_complete(false, :order => 'created_at DESC')
end

RSS Feed for Episode Comments 6 comments

1. Olivier Apr 29, 2007 at 08:55

hola


2. Vien May 02, 2007 at 11:37

wohoo, now i can shorten my code more, thx for the tip


3. Enrique Delgado May 07, 2007 at 12:14

Hi, I saw this railscast a while back and I've been meaning to ask something.

I like the dynamic find_by methods, but don't you lose the protection you get by the use of the question mark followed by variables?

Is "Task.find_all_complete(false)" less secure than "Task.find(:all, :conditions => ['complete = ?', false])" ? For example, if we were to plug a user-supplied value as the argument tot he dynamic find_by methods?


4. Ryan Bates May 07, 2007 at 15:01

@Enrique, good question. Thankfully Rails automatically quotes and escapes the input variable for the dynamic find by methods, so it is just as secure as using the full conditional statement with the question mark.


5. gd May 12, 2007 at 12:29

Ror is full of good things to help us on doing quick stuff


6. karl alexander Jun 11, 2007 at 13:55

Hi , This will be so usefull.Thank u so much.


7. Jonathan Jul 10, 2007 at 06:00

I wanted to reference the documentation because it took me quite a while to find this. http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001024

That's all about the with_scope method in ActiveRecord::Base. Hopefully someone else finds it useful for me to link it here.


8. Helena Dec 28, 2007 at 07:44

Hi,

Is there a way of using find_all_by type methods to replace conditions with wildcards in using LIKE or REGEXP?

Thanks very much for your help


9. Ryan Bates Jan 12, 2008 at 07:42

@Helena, you'll have to use a full find for that:

Task.find(:all, :conditions => ['name LIKE ?', name])

The shortcut find_by methods only work with equals comparison.


10. Samo Kralj Feb 27, 2008 at 08:45

Perhaps it would also be worth mentioning that you can combine conditions like this:
Task.find_all_by_complete_and_category_id(false, 1)


11. jc Dec 10, 2008 at 09:26

You can find more about this here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Under "Dynamic attribute-based finders"


12. Bungee Bob Sep 25, 2009 at 02:32

What does the "False" do? Thanks.


13. elomarns Oct 10, 2009 at 20:38

Now it's possible to write an even better version of last_incomplete:

def last_incomplete
  @task = Task.find_last_by_complete(false)
end


14. tiffany notes Jul 30, 2010 at 02:30

Great site. This could probably have the refactoring tag added t it.


15. optical fiber Cable Aug 01, 2010 at 23:59

I am very appreciate the issue that it can benefit to the low income guys, thanking for your sharing, it is wonderful.


16. solar collector solar water heater Aug 02, 2010 at 00:02

It's one of the most important facto to fix this time.


17. free directory list Aug 11, 2010 at 22:02

David Heinemeier Hansson


18. free directory list Aug 11, 2010 at 22:02

David Heinemeier Hansson


19. free directory list Aug 11, 2010 at 22:02

David Heinemeier Hansson


20. p90 workout Aug 12, 2010 at 09:14

This is one of the most incredible blogs Ive read in a very long time. The amount of information in here is stunning, like you practically wrote the book on the subject. Your blog is great for anyone who wants to understand this subject more. Great stuff; please keep it up!


21. uggs online Aug 13, 2010 at 22:07

Article is very nicely written.


22. jordan retro shoes Aug 16, 2010 at 00:30

The amount of information in here is stunning, like you practically wrote the book on the subject. Your blog is great for anyone who wants to understand this subject more. Thanks very much for your help


23. cool stuff Aug 25, 2010 at 02:15

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


24. louis vuitton shoes Aug 26, 2010 at 21:04

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


25. snow boots Aug 31, 2010 at 02:17

Thankfully Rails automatically quotes and escapes the input variable for the dynamic find by methods, so it is just as secure as using the full conditional statement with the question mark.


26. Cheap Supra Shoes Sep 01, 2010 at 00:01

I would like to view more such articles which I like very much.


27. GHD Hair Straighteners Sep 01, 2010 at 00:03

Excellent article and would like to view again.


28. GHD Australia Sep 01, 2010 at 00:03

I would like to access more of such contents


29. louis vuitton sunglasses Sep 01, 2010 at 22:29

Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.

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