ruby-on-rails - 用于过滤嵌套 has_many 关联的 ActiveRecord 查询

标签 ruby-on-rails ruby json postgresql activerecord

我想采用 ActiveRecord 模型并将关联一直过滤到顶层,以便符合特定条件的嵌套关联防止其父项出现在返回的记录中。

让我更好地解释一下。我有以下模型:

class SuperCategory < ActiveRecord::Base
  attr_accessible :name
  has_many :categories
end

class Category < ActiveRecord::Base
  attr_accessible :name
  belongs_to :super_category
  has_many :sub_categories
  has_many :books
end

class SubCategory < ActiveRecord::Base
  attr_accessible :name
  belongs_to :category
  has_many :books
end

class Book < ActiveRecord::Base
  belongs_to :sub_category
  belongs_to :category
  attr_accessible :show_me #boolean
end

在 SuperCategoriesController 中,我有:

class SuperCategoriesController < ApplicationController
  def index
    @super_categories = SuperCategory.all #here is where I want to query
    respond_with(@super_categories)
  end
end

点击 /super_categories.json 端点,我会得到看起来像这样的东西,它是 JSON 格式的整个树,因为我的 .jbuilder 部分添加了所有嵌套关联:

[
  { 
    name: "supercategory1",
    categories: [
      { 
        name: "category1",
        books: [
          { show_me: true },
          { show_me: true },
          { show_me: false }
        ]
      },
      {
        name: "category2",
        sub_categories: [ 
          { 
            name: "subcategory1",
            books:[
              { show_me: false }
            ]
          }
        ]
      }
    ]
  },
  {
    name: "supercategory2",
    categories: [
      {
        name: "category3",
        books: [
          { show_me: false }
        ]
      }
    ]
  }
]

如果我向 Controller 传递一个 show_me = true 的参数,我想在 Controller 中做什么,然后我不想显示任何书籍、子类别、类别和父类(super class)别其中有带有 show_me == false 的书籍。这是我尝试过的:

class SuperCategoriesController < ApplicationController
  def index
    if params[:show_me] == true
      @super_categories = SuperCategory.joins(:categories).joins(:sub_categories).where(:books => { show_me: true })
    end
    respond_with(@super_categories)
  end
end

这会产生错误,因为我不知道如何将 :categories 连接到 :sub_categories:books。我想要的结果是:

[
  { 
    name: "supercategory1",
    categories: [
      { 
        name: "category1",
        books: [
          { show_me: true },
          { show_me: true }
        ]
      }
    ]
  }
]

基本上,当我调用 @super_category.categories 时,应该只有那些嵌套书籍的类别,其中 show_me == true。类似地,如果有包含 show_me == true 书籍的子类别,它也应该返回那些。是否有 .joins.where 的 ActiveRecord 组合让我到达那里?

最佳答案

这应该有效:

SuperCategory.joins(categories: :books).where("books.show_me = ?", true)
   &
SuperCategory.joins(categories: :sub_categories, :books).where("books.show_me = ?", true)

关于ruby-on-rails - 用于过滤嵌套 has_many 关联的 ActiveRecord 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36440354/

相关文章:

java - 运行 allure 命令行生成命令时出错

ruby - 在 ruby​​ Gemfile 中指定 gem 的可选依赖项的正确方法

php - 从通过 JSON 从 PHP/Cakephp 传递到 Javascript/Jquery 的嵌套数组中获取数据

javascript - Javascript 中形成的 URL 无效

ruby-on-rails - 实现类似于 Kijiji 的类别和子类别的最佳方式是什么?

javascript - 为 jquery 添加 Rails 输入名称时遇到问题

ruby-on-rails - Capistrano + Thin + nginx 不允许用户使用 sudo howto?

ruby-on-rails - 如何从Nginx/Passenger中删除 "X-Runtime" header ?

ruby-on-rails - 将数据导入我的新 Rails 应用程序的最佳方式?

javascript - 如何从以纯文本形式显示的 API 中提取 JSON 数据?