7 Replies Latest reply on Jul 13, 2012 9:25 AM by Arquimidez Mora

    Having trouble with advanced conditions...

      I am trying to do a partial match on information in a model.

       

      Here is my code.

       

      name = Contact.find(:all, :conditions => {'name' =>'firstName', :op => 'LIKE'} => "'#{search_string}'",{'name' =>'lastName', :op => 'LIKE'| => "'#{search_string}'"},
        :op => 'OR', :order => ["firstName","lastName"])

       

      Unfortunately, it doesn't like the => after the first LIKE.

       

      It tells me "syntax error: Unexpected tASSOC".

       

      So what am I doing wrong?

       

      John Kocurek

        • Re: Having trouble with advanced conditions...

          name = Contact.find( :all,

           

          :conditions => {

           

             { 'name' =>'firstName', :op => 'LIKE'} => "'#'",    {'name' =>'lastName', :op => 'LIKE'} => "'#'"

           

          },

           

           

             

           

          :op => 'OR',

           

          :order => ["firstName","lastName"]

           

          )

           

           

           

          http://edgedocs.rhomobile.com/rhodes/rhom#advanced-queries

            • Re: Having trouble with advanced conditions...

              Right. That is what I did. It gives me that error on the => that follows the LIKE when I use the simulator.

                • Re: Having trouble with advanced conditions...

                  Your original hash is malformed. You have:

                   

                  name = Contact.find(:all, :conditions => {'name' =>'firstName', :op => 'LIKE'} => "'#{search_string}'",

                                                                             {'name' =>'lastName', :op => 'LIKE'| => "'#{search_string}'"},

                                                       :op => 'OR',

                                                       :order => ["firstName","lastName"])

                   

                   

                  What you should have is (and what Evgeny had posted):

                   

                  name = Contact.find(:all, :conditions => {{'name' =>'firstName', :op => 'LIKE'} => "'#{search_string}'",

                                                                             {'name' =>'lastName', :op => 'LIKE'| => "'#{search_string}'"}},

                                                       :op => 'OR',

                                                       :order => ["firstName","lastName"])

                   

                  Max.

              • Re: Having trouble with advanced conditions...

                Hi,

                 

                Yes, as USER03520 said, you just got two errors on your code:

                 

                On your last condition(the lastName) you have 'LIKE'| and it needs to be 'LIKE'}

                and after that you have:

                 

                => "'#{search_string}'"}, that in the end has      "},       and it nees to be         "}),

                 

                thats all

                  • Re: Having trouble with advanced conditions...

                    On Fri, Jul 13, 2012 at 10:19 AM, Arquimidez Mora <

                    • Re: Having trouble with advanced conditions...

                      Yeah, I didn't see that. Thanks for pointing this out.

                       

                      However, when I fix it, I get the same error at the same place.

                       

                      John Kocurek

                        • Re: Having trouble with advanced conditions...

                          Sorry, i didn't see that also you have to put all of this

                           

                          {'name' =>'firstName', :op => 'LIKE'} => "'#{search_string}'",{'name' =>'lastName', :op => 'LIKE'} => "'#{search_string}'"}

                           

                          into { }

                           

                          try copying this and should work fine:

                           

                          name = Contact.find( :all,

                                                         :conditions => {

                                                                                       {'name' =>'firstName', :op => 'LIKE'} => "#{search_string}",

                                                                                       {'name' =>'lastName', :op => 'LIKE'} => "#{search_string}"

                                                                               },

                                                         :op => 'OR',

                                                         :order => ["firstName","lastName"]

                                                        )