作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定以下模型:
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
has_many :ingredients, :through => :recipe_ingredients
end
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
end
class Ingredient < ActiveRecord::Base
end
如何在 Rails 3 中使用 Arel 执行以下 SQL 查询?
SELECT * FROM recipes WHERE NOT EXISTS (
SELECT * FROM ingredients WHERE
name IN ('chocolate', 'cream') AND
NOT EXISTS (
SELECT * FROM recipe_ingredients WHERE
recipe_ingredients.recipe_id = recipes.id AND
recipe_ingredients.ingredient_id = ingredients.id))
最佳答案
我不确定如何使用 Arel 或 ActiveRecord 进行关系除法。如果可以接受执行两个查询,则这将是等效的:
with_scope(includes(:recipes)) do
cream_recipes = Ingredient.where(:name => "cream").first.recipes
chocolate_recipes = Ingredient.where(:name => "chocolate").first.recipes
end
@recipes_with_chocolate_and_cream = cream_recipes & chocolate_recipes
或者您可以直接使用 find_by_sql 传递 SQL .
关于ruby-on-rails - Rails 3 等同于复杂的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3042308/
判断这2个相似的Uris实际上相同的标准方法是什么? var a = new Uri("http://sample.com/sample/"); var b = new Uri("http://sam
这个问题在这里已经有了答案: Why does "true" == true show false in JavaScript? (5 个答案) 关闭 5 年前。 可能我很困惑,但我无法理解这个愚蠢
我是一名优秀的程序员,十分优秀!