RAILS question, sql statements

Discussion in 'Ruby' started by bnews, Sep 27, 2005.

  1. bnews

    bnews Guest

    hey, i want to created an sql statement with rails for a search engine

    this is the code, u can search on first name and last name

    firm_id = @session[:user].id
    #last_name = params[:employee][:last_name]
    #first_name = params[:employee][:first_name]
    last_name = "Creytens"
    first_name = "b"
    @employee_pages, @employees = paginate :)employee, :conditions =>
    ["firm_id = ? and first_name like ? and last_name like ?" , firm_id,
    first_name, last_name] , :eek:rder_by => "last_name ASC", :per_page => 10)


    i have a few question:

    -how can i make, that the user doesnt need to fill in the full name, just
    1st or a couple of letters, in php i can use the % ==> '%first_name%', seems
    not to work here

    -the search engine is on the same page as the results, but the first time
    the parameters arent there, so i just want to show all employees, if the
    parameters are there then i show those.
    How can i check if the parameters are there, in php u can u isset(
    *_POST["last_name"] ), what is that here?
    bnews, Sep 27, 2005
    #1
    1. Advertising

  2. Hi :)
    > i have a few question:
    >
    > -how can i make, that the user doesnt need to fill in the full name, just
    > 1st or a couple of letters, in php i can use the % =3D=3D> '%first_name%'=

    , seems
    > not to work here


    You can use:

    @employee_pages, @employees =3D paginate :)employee, :conditions =3D>
    ["firm_id =3D ? and first_name like '%#{first_name}%' and last_name like
    '%#{first_name}%'" , firm_id, :eek:rder_by =3D> "last_name ASC", :per_page
    =3D> 10)


    > -the search engine is on the same page as the results, but the first time
    > the parameters arent there, so i just want to show all employees, if the
    > parameters are there then i show those.
    > How can i check if the parameters are there, in php u can u isset(
    > *_POST["last_name"] ), what is that here?


    if @params['last_name']
    #parameter given
    else
    #parameter not given
    end
    Bruno Celeste, Sep 27, 2005
    #2
    1. Advertising

  3. bnews

    Paul Guest

    You could also just add the % characters to the passed value and use
    the ? in the sql statement.

    q_firstname = '%' + first_name + '%'
    q_lastname = '%' + last_name + '%'

    @employee_pages, @employees = paginate :)employee, :conditions =>
    ["firm_id = ? and first_name like ? and last_name like ?" , firm_id,
    q_firstname, q_lastname] , :eek:rder_by => "last_name ASC", :per_page =>
    10)

    This way, the value passed in as first_name and last_name will be
    escaped properly.
    Paul, Sep 27, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,183
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    374
    Bart Nessux
    Feb 23, 2004
  3. Vince
    Replies:
    12
    Views:
    747
    Martin Gregorie
    Jan 21, 2008
  4. JT
    Replies:
    1
    Views:
    118
    Bob Barrows [MVP]
    Sep 27, 2005
  5. John Crichton
    Replies:
    6
    Views:
    262
    John Crichton
    Jul 12, 2010
Loading...

Share This Page