SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL query and quotes

    Hello guys, I need to use will_paginate and I have a silly issue with "conditions"

    I need to have the SQL code like below (with dynamic values on NOT IN condition):

    Code SQL:
    SELECT * FROM items WHERE (id NOT IN (0,1))  ORDER BY name LIMIT 0, 3

    so, having the string with the value to exclude on 'hidden_list'


    Code Ruby:
       @item_list = Item.paginate  :per_page => 3, :page => params[:page], :conditions => ['id NOT IN ?', "(#{hidden_list})"  ],:order => "name"

    but this generates the SQL code below

    Code SQL:
    SELECT * FROM items WHERE (id NOT IN '(0,1)')  ORDER BY name LIMIT 0, 3

    and with the quotes it doesn't work.

    What have I to change in order to delete the quotes ???


    THANK YOU and HAVE A NICE DAY!
    Alla prossima ...

  2. #2
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try
    Code ruby:
    @item_list = Item.paginate  :per_page => 3, :page => params[:page], :conditions => ['id NOT IN (?)', hidden_list],:order => "name"
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but it doesn't work. Actually now I don't have any SQL error but the query is:


    Code SQL:
    SELECT * FROM items WHERE (id NOT IN ('0,1'))  ORDER BY name LIMIT 0, 3

    so it compares the id with the string '0,1' and not with the single values.



    Any other idea ??? is it because hidden_list is a string ???


    THANKS
    Alla prossima ...

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by macsig View Post
    is it because hidden_list is a string ???
    Yes. An array will work with what pjleonhardt posted.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  5. #5
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks,
    so if hidden_list is an array it is gonna exclude all the values within the array. is it correct ???


    THANKS
    Alla prossima ...

  6. #6
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try and see.

    If you are stuck with hidden_list as a string:
    hidden_list.split(',')
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  7. #7
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, now it works (I had to cast them to integer)


    HAVE A GOOD 1!
    Alla prossima ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •