Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 复杂搜索设计_Mysql_Ruby On Rails 3_Search_Search Engine - Fatal编程技术网

Mysql 复杂搜索设计

Mysql 复杂搜索设计,mysql,ruby-on-rails-3,search,search-engine,Mysql,Ruby On Rails 3,Search,Search Engine,我正在使用mysql在rails3上实现配方搜索 搜索的思想是用户输入任意数量的配料,然后搜索输出建议,并按产品缺陷顺序排序 class Recipe < ActiveRecord::Base has_many :ingredients end # these records will be entered by user class IngredientType < ActiveRecord::Base has_many :ingredients end # this

我正在使用mysql在rails3上实现配方搜索

搜索的思想是用户输入任意数量的配料,然后搜索输出建议,并按产品缺陷顺序排序

class Recipe < ActiveRecord::Base
  has_many :ingredients
end

# these records will be entered by user
class IngredientType < ActiveRecord::Base
  has_many :ingredients
end

# this table is join table
class Ingredient < ActiveRecord::Base
  belongs_to :ingredient_type
  belongs_to :recipe
end
类配方
实现此搜索的最有效方法是什么? 你会推荐什么宝石或技术? 谢谢你的回答

  def self.from_ingredients ingredients
    count_sql = Ingredient.
        select('COUNT(*)').
        joins(:recipes_ingredients).
        where('`recipes_ingredients`.`recipe_id` = `recipes`.`id`').
        where('`ingredients`.`id` in (?)', ingredients).to_sql

    where("(#{count_sql}) > 0").
        order("((`recipes`.`ingredients_count`) - (#{count_sql})) ASC")
  end
通过在配方模型中创建这样的方法,我设法找到了解决方案