带有rails 4的嵌套范围

时间:2014-02-12 20:09:13

标签: ruby-on-rails ruby ruby-on-rails-4 scope named-scope

我有两个范围:with_category和simple_search。他们每个人都很好地单独工作,但我无法完全得到我需要的东西。

product.rb

has_many :categorizations, :dependent => :destroy
has_many :categories, :through => :categorizations

scope :with_category, lambda { |category_slug| 
    return {} if category_slug.blank?

    joins(:categorizations => :category).where('categorizations.category_id = ?', Category.find_by_slug(category_slug).id).uniq
  }

  scope :simple_search, lambda { |query|
    return {} if query.blank?

    where('"products"."name" ~* ? OR "products"."description" ~* ?', query, query)
  }

products_controller.rb

def index
  @products = Product.with_category(params[:category])
                     .simple_search(params[:query])
                     .paginate(:page => params[:page], :per_page => 10)
end

当我转到localhost:3000/products?category=apple-laptops时,我想抓住当前类别的所有产品。而不是这个,我的 with_category 范围被忽略,我可以看到所有产品。不知道为什么会这样。

从我的日志中:

  Product Load (0.9ms)  SELECT "products".* FROM "products" LIMIT 10 OFFSET 0
  Category Load (0.7ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."product_id" = $1  [["product_id", 2]]
  Category Load (0.8ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."product_id" = $1  [["product_id", 4]]
  Category Load (0.7ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."product_id" = $1  [["product_id", 10]]
  Category Load (0.8ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."product_id" = $1  [["product_id", 5]]
  Category Load (0.7ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."product_id" = $1  [["product_id", 18]]
  Category Load (0.7ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."product_id" = $1  [["product_id", 1]]
  Category Load (0.8ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."product_id" = $1  [["product_id", 3]]
  Category Load (0.7ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."product_id" = $1  [["product_id", 15]]

1 个答案:

答案 0 :(得分:1)

您是否尝试过像这样更改范围?

scope :with_category, lambda { |category_slug| 
    joins(:categorizations => :category).where('categorizations.category_id = ?', Category.find_by_slug(category_slug).id).uniq unless category_slug.blank?
}

scope :simple_search, lambda { |query|
    where('"products"."name" ~* ? OR "products"."description" ~* ?', query, query) unless query.blank?
}