> 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
I'm half tempted to say we should make case insensitive the effective default in postgres as it would be a more consistent experience at least. Given common pattern of dev with sqlite and deploy to heroku (postgres) it would avoid some of these sticking points.
To view this discussion on the web visit https://groups.google.com/d/msg/hobousers/-/sKR3-TdzBzUJ.
--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.
Owen Dall
Vice President | Chief Technology Officer
Barquin International
Office: 202.296.7147 | Mobile: tel:410.991.0811
Fax: 202.296.8903 | email: od...@barquin.com