You're probably looking for named scopes. They can be used to add
conditions to an association, like store.products, on the fly.
For instance, if you have these named scopes on your Product model:
named_scope :price_less_than, lambda { |p| { :conditions => ['price
<= ?', p] } }
named_scope :price_more_than, lambda { |p| { :conditions => ['price
>= ?', p] } }
... and a seach form with two fields, :price_max and :price_min, then
this code will let you filter your results:
proxy = store.products
proxy = proxy.price_less_than(params[:price_max]) unless params
[:price_max].blank?
proxy = proxy.price_more_than(params[:price_min]) unless params
[:price_min].blank?
...then you can use proxy anywhere you would have used store.products
(feed it to will_paginate, etc). If you have very specific conditions
that would clutter up the model, you can also use .scoped to create an
anonymous scope.
The benefit here is that the actual records aren't retrieved until you
try to actually access elements of proxy, for instance by iterating
over it.
--Matt Jones
On Jun 5, 12:03 pm, Yanni Mac <
rails-mailing-l...@andreas-s.net>
wrote: