首页 > 解决方案 > Rails 5 活动记录查询未显示 left_outer_joins 属性

问题描述

我正在尝试进行左外连接以获取相关属性,然后按相关属性进行排序。

我的问题是相关属性 pfeature_id 未在输出中列出。

我在这里找不到任何关于我可能做错的文章。

下面的示例 1 和 2,相关内容product_features.pfeature_id 未显示在列表中。

在示例 3 中,原始 sql 在 sqlite3 或 mysql 中运行,并显示了 pfeature_id。

我提到的页面之一是:https ://guides.rubyonrails.org/active_record_querying.html#left-outer-joins

有人能告诉我我做错了什么吗?

1.

albe@vamp398:/srv/test/brails/brail484b51$ dc exec web bundle exec rails console
Loading development environment (Rails 5.2.3)

irb(main):002:0>  b=Product.left_joins(:product_feature).select("products.id,
 products.name, product_features.pfeature_id").where("products.id>8").take(2)

  Product Load (0.7ms)  SELECT  products.id, products.name, 
product_features.pfeature_id FROM "products" LEFT OUTER JOIN "product_features" ON 
"product_features"."product_id" = "products"."id" WHERE (products.id>8) 
LIMIT ?  [["LIMIT", 2]]

=> [#<Product id: 9, name: "redKnife">, #<Product id: 10, name: "pf">]

2.

class Product < ApplicationRecord
  has_many :product_feature
  has_many :pfeature, through: :product_feature
  
  def self.pf
    # raw sql select using ...
    Product.find_by_sql \
    <<-SQL
      SELECT  products.id, products.name, product_features.pfeature_id FROM products LEFT OUTER JOIN 
        product_features ON product_features.product_id = products.id 
    SQL
  end
end

irb(main):005:0> b=Product.pf.last(4)
        
SELECT  products.id, products.name, product_features.pfeature_id FROM products
 LEFT OUTER JOIN product_features ON product_features.product_id = products.id

=> [#<Product id: 8, name: "11">, #<Product id: 9, name: "redKnife">, 
#<Product id: 10, name: "pf">, #<Product id: 10, name: "pf">]

3.

Run this sql in sqlite3 or MySQL:

SELECT  products.id, "products".name, product_features.pfeature_id FROM
 "products" LEFT OUTER JOIN "product_features" ON 
"product_features"."product_id" = "products"."id" 
WHERE (products.id>8) LIMIT 2

    id  name      pfeature_id
    9   redKnife  NULL
    10  pf        8
Tables:

  create_table "product_features", force: :cascade do |t|
    t.string "name"
    t.integer "product_id"
    t.integer "pfeature_id"
    ...
  end

  create_table "products", force: :cascade do |t|
    t.string "name"
    ...
  end

标签: ruby-on-rails-5

解决方案


问题是,如果 in 中的列不是在其上调用select的模型的属性之一,则不会显示这些列。select所有这些属性仍然包含在其中的对象中AR::Relation,并且可以像任何其他公共实例属性一样访问。前任:

products = Product.left_joins(:product_feature).select("products.id, products.name, product_features.pfeature_id").where("products.id > 8").take(2)
products.first.pfeature_id

为了验证,只需在控制台中运行这两行。你会得到你想要的输出。因为它是一个LEFT JOIN,你可能会得到nil一个product没有的输出product_features

如果要对输出进行排序,请记住排序列应包含NOT NULL值。前任:

products = Product.left_joins(:product_feature).select("products.id, products.name, product_features.pfeature_id").where("products.id > 8")

# sort with your desired column
sorted_products = products.sort_by(&:name)
# or
sorted_products = products.sort_by(&:pfeature_id) # make sure `pfeatured_id` is not null for any product

推荐阅读