does anyone know how i can bind the one variable to more than one placeholders in a statement like the following: @datafiles = Datafile.find(:all, :order => ''updated_on'', :conditions => [''LOWER(title) LIKE ? OR LOWER(filename) LIKE ?'', ''%'' + @params[''search''].downcase + ''%''], :limit => 20) i was under the impression that you could use ''?1'' to specify the first variable bound so you could have: @datafiles = Datafile.find(:all, :order => ''updated_on'', :conditions => [''LOWER(title) LIKE ?1 OR LOWER(filename) LIKE ?1'', ''%'' + @params[''search''].downcase + ''%''], :limit => 20) but that doesn''t seem to work. maybe inserting a block that will ''yield'' the variable in all those places but i am only new to a lot of this stuff. any ideas? -felix
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 10, 2005, at 6:14 PM, Felix McCoey wrote:> does anyone know how i can bind the one variable to more than one > placeholders in a statement like the following: > > @datafiles = Datafile.find(:all, :order => > ''updated_on'', :conditions => > [''LOWER(title) LIKE ? OR LOWER(filename) LIKE ?'', ''%'' + > @params[''search''].downcase + ''%''], :limit => 20)Use named bind variables: Datafile.find(:all, :conditions => [ ''LOWER(title) LIKE :term OR LOWER(filename) LIKE :term'', { :term => "%#{params[:search].downcase}%" } ], :order => ''updated_on'', :limit => 20 ) Moving this into a method of Datefile makes your controller action easier on the eyes, too: class Datafile < ActiveRecord::Base def self.search(term) find(:all, ...) end end class DataController < ApplicationController def search @hits = Datefile.search(params[:term]) end end Regards, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDSx1LAQHALep9HFYRAmstAKC+U4dbnVu3MeMukSjOnSNFwynitQCfS9PN 4AktyCQ0wtehuP1MDlZDtJk=4eTZ -----END PGP SIGNATURE-----
You can provide the variables as a hash:
@datafiles = Datafile.find(:all, :order => ''updated_on'',
:conditions => [''LOWER(title) LIKE :search_string OR LOWER
(filename) LIKE :search_string'',
{:search_string => ''%'' +
@params[''search''].downcase +
''%''}], :limit => 20)
See the "Conditions" section in http://api.rubyonrails.com/classes/
ActiveRecord/Base.html, and the source.
-- Michael Daines
On Oct 10, 2005, at 6:14 PM, Felix McCoey wrote:
> does anyone know how i can bind the one variable to more than one
> placeholders in a statement like the following:
>
> @datafiles = Datafile.find(:all, :order =>
> ''updated_on'', :conditions =>
> [''LOWER(title) LIKE ? OR LOWER(filename) LIKE ?'',
''%'' +
> @params[''search''].downcase + ''%''],
:limit => 20)
>
> i was under the impression that you could use ''?1'' to
specify the
> first
> variable bound so you could have:
>
> @datafiles = Datafile.find(:all, :order =>
> ''updated_on'', :conditions =>
> [''LOWER(title) LIKE ?1 OR LOWER(filename) LIKE ?1'',
''%'' +
> @params[''search''].downcase + ''%''],
:limit => 20)
>
> but that doesn''t seem to work.
>
> maybe inserting a block that will ''yield'' the variable in
all those
> places but i am only new to a lot of this stuff.
>
> any ideas?
>
> -felix
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>