Сложный поисковый запрос - параметры зависят от записи - PullRequest
0 голосов
/ 06 декабря 2011

В моем приложении на Rails 3.1 есть модель, которая содержит эти атрибуты (все являются целыми числами):

  • first_group_count
  • second_group_count
  • third_group_count
  • second_group_max_age
  • third_group_max_age

В основном они относятся к 3 группам людей (участников), чей возраст должен быть

  • для first_group: большечем second_group_max_age
  • для second_group: от third_group_max_age до second_group_max_age
  • для third_group: от 0 до third_group_max_age

Теперь мне нужно выполнить поиск по этой модели, и у меня в запросе есть массив значений, подобных этому: [3, 5, 13, 16, 21]

Мне нужно найти записи, в которых всего 5 человек (first_group_count + second_group_count +third_group_count = 5) и иметь место в своих группах.(надеюсь, вы можете получить это)

Пример:

Запрос: [3, 7, 14]

  1. длязаписи с second_group_max_age=8 и third_group_max_age=4 должны найти записи с:

    • first_group_count = 1
    • second_group_count = 1
    • third_group_count = 1
  2. для записей с second_group_max_age=13 и third_group_max_age=0 он должен найти записи с:

    • first_group_count = 1
    • second_group_count = 2
    • third_group_count = 0

Что было бы лучшим решением этой проблемы?Буду очень признателен за любую помощь!

1 Ответ

0 голосов
/ 07 декабря 2011

Хорошо.:)

Звучит так, что вам просто нужно пропустить эти значения each через case, например, так:

def find_groups(x, y)
 # not sure if you want these dynamic or not..
 first_group_max_age = x
 second_group_max_age = y
 # start by making sure these are at a 0 count
 first_group = 0
 second_group = 0
 third_group = 0

 params[:values].each do |value|
   age = case value
     when > second_group_max_age then first_group += 1
     when > third_group_max_age && < second_group_max_age then second_group +=1
     when < third_group_max_age then third_group +=1
   end
 end
end

# now we have an idea about who many are in each group.. I'm still unsure about what your project is trying to accomplish from this point.. Enlighten me, and we can take this further..
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...