Ruby on Rails Tuesday, March 31, 2015

Thanks Dave.

Yes, ORing over values for the same attribute is not a problem, that can be done with something like name=['Doris', 'Bob'] or similar, which would generate an SQL IN statement (where name in ('Bob', 'Doris')). The problem is when ORing two different attributes or arbitrarily mixing ANDs and ORs.

Anyhow, I ran in to ransack, which seems to be a promising starting point for this kind of functionality, if not supported out of the box. Haven't had time to try it out yet.

 - fred

On Monday, March 30, 2015 at 11:15:08 PM UTC+3, Dave Aronson wrote:
On Mon, Mar 30, 2015 at 3:55 PM, Fredrik Boström <fbos...@gmail.com> wrote:

> Maybe I expressed myself a bit unclearly. I'm not trying to reinvent SQL or
> create a new database implementation, that's insane!

Okay.  Maybe I jumped the gun a bit too; I've seen lots of zealous
people, at the stage where they "know enough to be dangerous", head
down such a path.  :-)

> What I'm after is a way to
> do more complex filtering of the result set using query parameters than what
> ActiveRecord now supports.

Perhaps you could take query params that included embedded ANDs and
ORs and some parentheses, and pass them off to some gem that can
handle that on each field.  So, you would wind up with a URL like:

GET /api/users?name=Doris OR Fred&location=Stockholm&hobbies=horseback
OR swimming

Then, inside your controller (or better yet, User.search method or
UserSearch.call or whatever), have it take whatever searchable params
were given and hand them off to, say, Solr or ThinkingSphinx or
whatever.

Or-ing THOSE together, so as to look for anyone who's in Stockholm OR
is interested in horseback or swimming, is another story.  I think you
can do that with Solr (been a while since I've used it) but of course
the handling and syntax would be different.

In summary, check out the assorted search gems, especially full-text
search, see what they can do for you, and base your syntax on what can
be easily broken up into the kinds of pieces your preferred search gem
expects.

-Dave

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

--
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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/824603c9-7c2b-4ee4-b3b2-e6c6b38d643c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment