简体   繁体   中英

ruby on rails 3 - advanced searches using gems

I have a User model with columns for Local, Skype, and Phone (boolean, 't' or 'f') and Gender (string, 'Male' or 'Female'). Each user can have many skills in the associated SkillMap model which has the skill_id and direction (string, either "Teach" or "learn")

I've been messing around with MetaWhere, Squeel, and RanSack trying to figure out which will allow me to search these users in the nicest, most readable, and most rails-like way possible. Can anyone point me to the most appropriate gem and maybe give me some tips on how to build it?

Currently, each time a search is executed an object like the one below is created in the searches table (sorry for the poor formatting, I don't know how to save an array without those dashes)

id|user_id|skype|local|phone|teach_skill_ids|learn_skill_ids|gender
37|628|t|t|f|---- '795'|---- '789'- '771'- '827'|Male

So, in this case I want to find any users who's skype OR local flag are true (ignoring phone = 'f'), who's gender flag is 'Male', AND who have an associated skill_map where direction = 'Teach' and skill_id= 795, AND who have a skill_map where direction = 'Learn' and skill_id is IN (789,771,827)

I can take a stab at writing the SQL if that will make it clearer but I only know how to do it using terrible subqueries (DBAs might want to skip this part)

SELECT * FROM users 
WHERE gender = 'Male'
AND (local = 't' OR skype = 't')
AND id IN 
    (SELECT user_id FROM skill_maps
     WHERE direction = 'Teach' 
     AND id = 795)
AND id IN
    (SELECT user_id from skill_maps
     WHERE direction = 'Learn'
     AND id IN (789,771,827))

If any of these fields are blank or false, I do not want to include them as a search condition.

Anyway, thats my question. Thanks for having a look!

Cheers, Yonah

I think you should use a search engine like thinking sphinx or elasticsearch . Thinking sphinx is easy to use and setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM