gpt4 book ai didi

ruby-on-rails - Rails 选择 GROUP 中 COUNT 最高的对象

转载 作者:行者123 更新时间:2023-12-03 15:10:42 24 4
gpt4 key购买 nike

目标是选择最常使用 StoreCoupon

目前,我有这个,它有效(分解以供解释):

# coupon.rb
has_many :redemptions
has_and_belongs_to_many :stores

def most_popular_store
stores.find( # Return a store object
redemptions # Start with all of the coupon's redemptions
.group(:store_id) # Group them by the store_id
.count # Get a hash of { 'store_id' => 'count' } values
.keys # Create an array of keys
.sort # Sort the keys so highest is first
.first # Take the ID of the first key
)
end
###

它是这样使用的:
describe 'most_popular_store' do
it 'returns the most popular store' do
# Create coupon
coupon = FactoryGirl.create(:coupon)

# Create two different stores
most_popular_store = FactoryGirl.create(:store, coupons: [coupon])
other_store = FactoryGirl.create(:store, coupons: [coupon])

# Add redemptions between those stores
FactoryGirl.create_list(:redemption, 2, coupon: coupon, store: other_store)
FactoryGirl.create_list(:redemption, 5, coupon: coupon, store: most_popular_store)

# Verify
expect(coupon.most_popular_store.title).to eq most_popular_store.title
end
end

就像我说的,该方法有效,但它看起来像猴子补丁。如何重构我的 most_popular_store 方法?

最佳答案

我认为你的方法实际上不起作用。 count 为您提供一个散列,键为 store_ids,值作为计数,然后您在散列上运行 keys,它为您提供一个 store_ids 数组。从那时起,您已经失去了计数,您正在按 store_ids 排序并获取第一个。您的测试通过的唯一原因是您在另一个之前创建了流行商店,因此它的 id 较低(sort 默认情况下按升序排序)。要获得正确的结果,请进行以下更改:

redemptions       # Start with all of the coupon's redemptions
.group(:store_id) # Group them by the store_id
.count # Get a hash of { 'store_id' => 'count' } values
.max_by{|k,v| v} # Get key, val pair with the highest value
# output => [key, value]
.first # Get the first item in array (the key)

关于ruby-on-rails - Rails 选择 GROUP 中 COUNT 最高的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31618823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com