On Friday, March 15, 2013 7:31:26 AM UTC-7, Anthony DeFreitas wrote:
>
> Ok, getting closer. I found this
> post<http://stackoverflow.com/questions/6833281/sql-where-clause-that-begins-with>which
> says if I remove the leading '%' in the query it will find entries
> that 'Begin' with what is typed. This didn't work for me though and my
> query follows the same format:
>
> SubCategory.where("name like ?", "#{params[:q]}%")
>
On Friday, March 15, 2013 7:44:57 AM UTC-7, Anthony DeFreitas wrote:
> Sorry for not being so precise.
>
>>
> Processing by SubCategoriesController#names as JSON
>
>> Parameters: {"q"=>"a"}
>
>> SubCategory Load (0.8ms) SELECT "sub_categories".* FROM
> "sub_categories" WHERE (name LIKE 'a%')
"Didn't work for me" is not very descriptive. However, I'm going to take a
wild guess and assume that your subcategory names begin with capital
letters, and your database's LIKE is case sensitive, which would mean
you're now getting no rows found, instead of all rows when you were using
the wrong parameter name.
If that's the case, then you need to make your query case insensitive. In
PostgreSQL you could use ILIKE. I'm not sure if that's standard, though.
A more standard way to do make the query case insensitive would be:
SubCategory.where("LOWER(name) like LOWER(?)", "#{params[:q]}%")
Or, according to a bit of googling, this would automatically do a case
insensitive query correctly for the current database:
subcats=Subcategory.arel_table
Subcategory.where(subcats[:name].matches("%#{params[:q]}%"))
But I have not tried that mechanism myself.
Jim Crate
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/c-99scPUCMwJ.
For more options, visit https://groups.google.com/groups/opt_out.