搜索多个数据源

时间:2014-08-02 21:35:51

标签: ruby ruby-on-rails-3 algorithm hashmap

我正在寻找汽车搜索。我想搜索多个api /数据库并输出符合我输入值的十大汽车 - 品牌,型号,年份,颜色...... 我的搜索模型看起来像这样

##search.rb  my model## 

def candidates  
  @cars ||= get_cars
end

def car_databsae_one
   client = ...
end

def car_databsae_two
   client = ...
end

def car_databsae_three
   client = ...
end

def car_databsae_four
   client = ...
end

def car_databsae_five
   client = ...
end

private

def search_car_database_one
    ... returns results that match request to db1
end

def search_car_database_two
    ... returns results that match request db2 +
   if a car is both one and two merge the information 
end

def search_car_database_three
     ... returns results that match request db3 +
   if a car is one, two, and three merge the information
   or one and three
   or two and three
end  

def search_car_database_four
    ...
end

def search_car_database_five
    ...
end

def get_cars
  all_cars = search_car_database_five
end

是否有更好的方法可以立即搜索所有api并提取数据。每次在多个数据库中看到汽车时,都会将数据添加到该汽车的散列中。

1 个答案:

答案 0 :(得分:1)

所以,每个"数据库"实际上是一个API?那些API定义在哪里以及如何定义?

我认为更明智的解决方案不会明确定义所有这些。我认为你更明智地拉动这些数据库"到一个列表(数组),并有一个迭代这些的方法。从那里,防止重复结果取决于您和您的代码。

相关问题