Mysql 复杂搜索设计
我正在使用mysql在rails3上实现配方搜索 搜索的思想是用户输入任意数量的配料,然后搜索输出建议,并按产品缺陷顺序排序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
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
通过在配方模型中创建这样的方法,我设法找到了解决方案