了解 "where" 以及如何在 Rails 上的 Ruby 中使用它
Understanding the "where" and how to use it in Ruby on Rails
我有一个名为 product_variations
的 table,这个 table has_many products
.
当我转到 Rails 控制台时,我可以这样做:
2.6.3 :036 > var = ProductVariation.first
ProductVariation Load (0.2ms) SELECT `product_variations`.* FROM `product_variations` ORDER BY `product_variations`.`id` ASC LIMIT 1
=> #<ProductVariation id: 16, product_id: 1024, color_id: 19, quantity: 1, size: "GG", sub_sku: 10, created_at: "2020-02-18 14:41:46", updated_at: "2020-02-18 14:41:46">
2.6.3 :037 > var.size
=> "GG"
所以,这是有效的。但是当我尝试使用 where
在那里找到一些东西时,我得到了这个:
2.6.3 :038 > var = ProductVariation.where(product_id: 1024, sub_sku: 10)
ProductVariation Load (0.4ms) SELECT `product_variations`.* FROM `product_variations` WHERE `product_variations`.`product_id` = 1024 AND `product_variations`.`sub_sku` = 10 LIMIT 11
=> #<ActiveRecord::Relation [#<ProductVariation id: 16, product_id: 1024, color_id: 19, quantity: 1, size: "GG", sub_sku: 10, created_at: "2020-02-18 14:41:46", updated_at: "2020-02-18 14:41:46">]>
2.6.3 :039 > var.size
(0.2ms) SELECT COUNT(*) FROM `product_variations` WHERE `product_variations`.`product_id` = 1024 AND `product_variations`.`sub_sku` = 10
=> 1
请注意,当我尝试使用 var.size
时,控制台会在数据库中进行另一次搜索,但现在使用 SELECT COUNT(*)
而不是 SELECT
,输出为 1
(而应该是 GG
,为什么?)。据我目前所了解的,where
可以 return 很多结果。这就是为什么它使用 COUNT
?但是我该如何使用结果呢?在我工作的这个应用程序中,在 product_variations
table 上,我将只有一个与 product_id
和 sub_sku
的匹配,所以我希望它 return 这一行,但不知道如何使用 where
来做到这一点。
您可以在 where
之后使用 .first
var = ProductVariation.where(product_id: 1024, sub_sku: 10).first
或使用find_by方法
var = ProductVariation.find_by(product_id: 1024, sub_sku: 10)
它会做同样的事情
我有一个名为 product_variations
的 table,这个 table has_many products
.
当我转到 Rails 控制台时,我可以这样做:
2.6.3 :036 > var = ProductVariation.first
ProductVariation Load (0.2ms) SELECT `product_variations`.* FROM `product_variations` ORDER BY `product_variations`.`id` ASC LIMIT 1
=> #<ProductVariation id: 16, product_id: 1024, color_id: 19, quantity: 1, size: "GG", sub_sku: 10, created_at: "2020-02-18 14:41:46", updated_at: "2020-02-18 14:41:46">
2.6.3 :037 > var.size
=> "GG"
所以,这是有效的。但是当我尝试使用 where
在那里找到一些东西时,我得到了这个:
2.6.3 :038 > var = ProductVariation.where(product_id: 1024, sub_sku: 10)
ProductVariation Load (0.4ms) SELECT `product_variations`.* FROM `product_variations` WHERE `product_variations`.`product_id` = 1024 AND `product_variations`.`sub_sku` = 10 LIMIT 11
=> #<ActiveRecord::Relation [#<ProductVariation id: 16, product_id: 1024, color_id: 19, quantity: 1, size: "GG", sub_sku: 10, created_at: "2020-02-18 14:41:46", updated_at: "2020-02-18 14:41:46">]>
2.6.3 :039 > var.size
(0.2ms) SELECT COUNT(*) FROM `product_variations` WHERE `product_variations`.`product_id` = 1024 AND `product_variations`.`sub_sku` = 10
=> 1
请注意,当我尝试使用 var.size
时,控制台会在数据库中进行另一次搜索,但现在使用 SELECT COUNT(*)
而不是 SELECT
,输出为 1
(而应该是 GG
,为什么?)。据我目前所了解的,where
可以 return 很多结果。这就是为什么它使用 COUNT
?但是我该如何使用结果呢?在我工作的这个应用程序中,在 product_variations
table 上,我将只有一个与 product_id
和 sub_sku
的匹配,所以我希望它 return 这一行,但不知道如何使用 where
来做到这一点。
您可以在 where
之后使用 .firstvar = ProductVariation.where(product_id: 1024, sub_sku: 10).first
或使用find_by方法
var = ProductVariation.find_by(product_id: 1024, sub_sku: 10)
它会做同样的事情