Browse files

Query guide: lambdas must be used when working with scopes

  • Loading branch information...
1 parent 5b19579 commit 28b1642561cddc1f8650f7a8c740ece259db913b @radar radar committed Dec 23, 2010
Showing with 12 additions and 0 deletions.
  1. +12 −0 railties/guides/source/active_record_querying.textile
12 railties/guides/source/active_record_querying.textile
@@ -763,6 +763,18 @@ Or on an association consisting of +Post+ objects:
category.posts.published => [published posts, belonging to this category]
+h4. Working with times
+If you're working with dates or times within scopes, due to how they are evaluated, you will need to use a lambda so that the scope is evaluated every time.
+ class Post < ActiveRecord::Base
+ scope :last_week, lambda { where("created_at < ?", ) }
+ end
+Without the +lambda+, this will only be called once.
h3. Dynamic Finders
For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +first_name+ on your +Client+ model for example, you get +find_by_first_name+ and +find_all_by_first_name+ for free from Active Record. If you have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+ methods.

0 comments on commit 28b1642

Please sign in to comment.