On Sep 8, 2011, at 3:40 PM, Jim Harvey wrote:
> Good point, Kevin. It is indeed a case sensitive issue with postgres. Is
> there any way I can set a case insensitive flag at the "hobo level"? The data
> itself is mixed case and I would like to continue using apply_scopes if I
> can...
Not at present. You can use apply_scopes with any scope, though - so you could
define one for your case (in model.rb):
scope :name_icontains, lambda { |x| where(['name LIKE ?', "%#{x}%"]) }
(this is the Rails 3 flavor)
As an alternative, you could use the auto-generated search scope, which may
(depending on your application) be better anyways:
Model.apply_scopes(:search => [params[:q], :name])
The search scope does some additional processing to make searches more
intuitive in most cases:
- splits the query on spaces and looks for each word individually. This matches
records that contain the search terms with intervening text.
- if you pass more than one field, constructs a query that allows any record
that has the search terms in any combination of fields. For instance, if you've
got records with a 'name' and a 'description' field then this:
Model.search('foo bar', :name, :description)
will match a record with 'foo' in the name and 'bar' in the description.
As to the larger question, I'm not 100% sure what the best approach is. Off the
top of my head, here are some possibilities:
- add a global configuration option to switch 'LIKE' to 'ILIKE' in generated
scopes. Probably a really bad idea.
- add a model-level configuration option: better, since some models might
prefer case-sensitive comparison vs. others.
- add a *field*-level configuration option: this is much chattier than the
previous, but would be the most powerful. Essentially, you'd add an
:ignore_case => true flag to the field declaration. Also possibly useful in
dealing with :unique validation.
I have to admit, I don't really have much of a perspective on this - most of my
development work has been on MySQL, where the case-sensitivity issue is (for
better or for worse) totally absent. Any Postgres, etc. users want to weigh in?
--Matt Jones
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en.