【问题标题】:Rails render XML of a INNER JOINRails 呈现 INNER JOIN 的 XML
【发布时间】:2023-03-17 13:50:01
【问题描述】:

我正在使用 2 个连接模型

class Product < ActiveRecord::Base
  has_and_belongs_to_many :providers
end
class Provider < ActiveRecord::Base
  has_and_belongs_to_many :products
end

我的控制器看起来像这样

class ProductsController < ApplicationController
    @products = Product.find( 
      :all, 
      :joins => :providers, 
      :select => "providers.id, providers.title, products.id, products.title, products.price", 
      :limit => 10)
    respond_to do |format|
      format.xml  { render :xml => @products }
      format.json { render :json => @products }
    end
  end
end

@products 未按预期呈现。 XML 文件中仅显示 Product 模型的列。我尝试将 format.xml 行更改为

format.xml  { render :xml => @products.to_xml( :include => :providers) }

但这不是我想要的。你可以查看我的 5 列 SQL 查询

SELECT providers.id, providers.title, products.id, products.title, products.price 
FROM `products` 
INNER JOIN `products_providers` ON `products_providers`.product_id = `products`.id 
INNER JOIN `providers` ON `providers`.id = `products_providers`.provider_id 
LIMIT 10

但在我的 XML 中只显示了 3 个。 to_xml 方法还会生成一些外部 SQL 请求,我不希望这样。

谁能告诉我如何告诉 Rails 呈现我的所有 SQL 字段?我也希望代码得到优化。

理想的 XML/JSON 设计应该是

<products type="array">
<product>
  <id type="integer">1</id>
  <price type="decimal">9.99</price>
  <title type="string">Sanke Rolex</title>
  <provider>
    <id type="string">1</id>
    <title type="string"></title>
  </provider>
</product>
</products>

谢谢!

【问题讨论】:

    标签: ruby-on-rails xml inner-join render


    【解决方案1】:

    当您明确表示希望 XML 输出包含所有属性时,我不明白为什么要将自己限制在 :select 参数中的某些列。

    最优化的代码是这样的:

    @products = Product.all(:include => :providers, :limit => 10)
    respond_to do |format|
      format.xml  { render :xml => @products.to_xml(:include => :providers) }
      format.json { render :json => @products.to_json(:include => :providers) }
    end
    

    我在查找器中使用 :include 而不是 :joins,这意味着 AR 将使用 2 个 SQL 查询来获取第一个产品,然后是提供程序,这对于更大的表来说比连接更快。

    要在 XML 输出中排除某些私有列,请使用 :except

    @products.to_xml(
      :except => [:price],
      :include => { :providers => {:except => [:title]} }
    )
    

    你几乎总是想要这个,因为每个模型都包含不公开的信息。

    【讨论】:

      【解决方案2】:

      如果我想要一个非常具体的输出,我会使用 Builder template 而不是 render :xml 快捷方式。这很简单。

      【讨论】:

      • JSON 格式呢?我想创建一个可以转换为任何格式的通用@variable - XML、JSON
      • 我如何从两个表格中访问信息。我看到的唯一方法是你使用 AS - SELECT products.id AS product_id...
      猜你喜欢
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 2010-12-03
      • 2010-10-08
      相关资源
      最近更新 更多