#4
Mar 12, 2007

Move Find into Model

Move a find into the model to clean up the controllers and remove duplication. Also see how you can call these custom find methods through an association.
Download (4.8 MB, 2:03)
alternative download for iPod & Apple TV (2.8 MB, 2:03)
# tasks_controller.rb
def index
  @tasks = Task.find_incomplete
end

# models/task.rb
def self.find_incomplete
  find_all_by_complete(false, :order => 'created_at DESC')
end

# projects_controller.rb
def show
  @project = Project.find(params[:id])
  @tasks = @project.tasks.find_incomplete
end

RSS Feed for Episode Comments -6 comments

1. James May 28, 2007 at 22:17

Very nice example, great work!


2. Lucky Sep 08, 2007 at 10:14

Hi Ryan,

On this topic "Move Find into Model", I have queries which I hope you could help to enlighten me:

I provide CRUD of books. First, user will have to select the action (i.e. C, R, U or D) from the menu. Then, a search screen is displayed for user to enter the book title/s which s/he would like to check.

The search string could contain more than one word, * (0 or more character wild card), ? (single character wild card) and so on.

In this case, should the formatting and validation of the search string be also embedded in the Book model, together with the Find in the Book model? If not, where should this be included?

Thank you for your help.


3. Ryan Bates Sep 11, 2007 at 08:11

@Lucky, yes, I would put the handling of the search wild cards into the model. That's logic that doesn't belong in the controller.


4. James Sep 22, 2007 at 16:09

I am curious about the self.find...

Why is it that this method requires "self." ?


5. James Sep 23, 2007 at 09:46

I found an answer to something that has been confounding me about the self. thing. I am learning about OO programming, so I admit that this may seem obvious to a lot of people. Please excuse my ignorance.

Here is a link regarding how this works.
http://wiki.rubyonrails.org/rails/pages/RubyHeadScratching

Thank you Ryan for all the excellent work in your railscasts. I really appreciate the patience and clear mindedness that you demonstrate in your screencasts. You express yourself as a joyful and brilliant person.


6. Juan Carlos Rodríguez Dec 06, 2007 at 23:36

Great job! Thanks


7. john May 01, 2008 at 03:29

i know this is wrong place to write this but i need a solution i want to know how mapping should be done mine case is " A has many B , A has many C, every B has some C which is added to above list which is related to A. so tell me the way to map them . i have mapped B and C to A


8. check valves Nov 16, 2009 at 19:40

Valves supplier, valves manufacturer, fittings, ball valves, butterfly valves, sanitary butterfly valves, sanitary ball valves, sanitary fittings, valves manufacturers, ball valve, butterfly valve, sanitary unions, sanitary clamp unions, sanitary check valves, sanitary sight glasses, flanges, butterfly valve handles


9. Seth Galvarro Jan 07, 2010 at 11:12

I'm a newbie to rails and adding this post to clarify the railscast. I was wondering why the def was called "self.find_incomplete" when the original code was "find_all_by_complete". It took me a few minutes, but it's called "incomplete" because the data being queried is a boolean condition and the query is looking for the "false" conditions (i.e. tasks that are not completed) ergo the naming if "incomplete". Yes, a Homer Simpson DOH! moment...


10. pump shoes Aug 03, 2010 at 01:01

i love [url=http://www.gavots.com] armani sunglasses [/url]very much


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

It is amazing! Thanks!


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

It is amazing! Thanks!


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

It is amazing! Thanks!


14. uggs online Aug 13, 2010 at 22:10

Article is very nicely written.


15. Air Jordan Retro Aug 19, 2010 at 00:24

It is amazing! Thanks! This was a really useful episode, ROR really is awesome in that it lets you write code like that! Thanks for very good information. Excellent article that will provide the incentive and basis for my works.I wonder if I can mention the article as a bibliographic reference in my work. Thanks!


16. selma Aug 19, 2010 at 13:40

hello thank you


17. louis vuitton shoes Aug 26, 2010 at 21:18

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


18. snow boots Aug 31, 2010 at 02:15

This was a really useful episode, ROR really is awesome in that it lets you write code like that! Thanks for very good information. Excellent article that will provide the incentive and basis for my works.


19. louis vuitton sunglasses Sep 01, 2010 at 22:34

YES! Great piece, keep up the terrific work. This is the type of information that should gain recognition for it’s craft. More writers should learn from you. This is right on the money.

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