On Feb 8, 2010, at 8:35 PM, Brian Corbin wrote:
I think I have search working by doing this:
@location_products = @location.location_products unless !
params[:search].nil?
@location_products =
@location.location_products.find(:all, :conditions => "product_id in
(select id from products where name like '%" + params[:search] +
"%')") unless params[:search].nil?
Note that this could be shortened to:
if params[:search].blank?
@location_products = @location.location_products
else
@location_products = @location.location_products.find(...)
end
Or shorter (if location_products is an association, as I suspect):
@location_products = @location.location_products
unless params[:search].blank?
@location_products = @location_products.find(...)
end
Or, keeping pagination:
@location_products = @location.location_products
unless params[:search].blank?
@location_products = @location_products.scoped(:include
=> :products, :conditions => ['products.name LIKE ?',
"%#{params[:search]}%"])
end
(the above is secure against SQL injection, as the ? interpolation
will catch nasties)
Or (and this may be going too far):
on LocationProduct:
named_scope :for_products_named_like, lambda { |n| n.blank? {} :
{ :include => :products, :conditions => ['products.name LIKE ?', "%#{n}
%"] } }
then the controller:
@location_products =
@location.location_products.for_products_named_like(params[:search])
----
Can you tell I used to be a Perl programmer? The "reduce it to one
line" habit dies hard...
--Matt Jones
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en.