diff options
author | Ryan Bigg <radarlistener@gmail.com> | 2010-12-23 14:50:40 +1000 |
---|---|---|
committer | Ryan Bigg <radarlistener@gmail.com> | 2010-12-23 14:50:40 +1000 |
commit | 28b1642561cddc1f8650f7a8c740ece259db913b (patch) | |
tree | ccf26aa2984c93a31c92861aa0c2971066e3dc31 /railties | |
parent | 5b19579ab3a2ea95f8354cc6aa407bf2e5264d26 (diff) | |
download | rails-28b1642561cddc1f8650f7a8c740ece259db913b.tar.gz rails-28b1642561cddc1f8650f7a8c740ece259db913b.tar.bz2 rails-28b1642561cddc1f8650f7a8c740ece259db913b.zip |
Query guide: lambdas must be used when working with scopes
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/active_record_querying.textile | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index fe6d284239..25f3723b93 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/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] </ruby> +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. + +<ruby> + class Post < ActiveRecord::Base + scope :last_week, lambda { where("created_at < ?", Time.zone.now ) } + end +</ruby> + +Without the +lambda+, this +Time.zone.now+ 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. |