我在Rails应用程序中有两个模型: <core-datepicker class="start-date" default-date="" start-date="18-03-2019" end-date="28-03-2020" highlight-from="20-03-2019" highlight-to="20-04-2019" end-show="true" fly-out="true" value="dateRange.startDate" cb-event-id="cal-selector:select-start-date" unavailable-dates="dateRange.unavailabiltyStartDates" selected-month-view="dateRange.startDateSelectedMonthView" show-month-toggle="::dateRange.showMonthToggle" show-single-month=""><!----><div ng-class="::{'has-monthly-toggle': isMonthToggleVisible()}"><div bindonce="" class="datepicker-wrapper r scrollable value-selected" ng-class="{ scrollable: !device.isPhone(), mobile: device.isPhone(), 'value-selected': value, 'six-rows': checkIf6Rows()}" style="transition-timing-function: cubic-bezier(0.1, 0.57, 0.1, 1); transition-duration: 0ms; transform: translate(0px, 0px) translateZ(0px);"><!----><!----><ul ng-if="!device.isPhone()"><!----><li bindonce="" ng-repeat="i in _monthViews" ng-class="{ 'starting-month': checkIfIsSame(getDate(i), highlightFrom, 'month'), 'selected-month': checkIfIsSame(getDate(i), value, 'month'), 'highlight-on': canHighlight(i) }" class="calendar-view starting-month selected-month"><h1 class="month-name">March 2019</h1><ul class="week-days"><!---->
有很多home
。
每个speed_tests
都具有以下属性:speed_test
和hostname
。
我想对主机名进行分组并显示平均下载次数。
首先,我按城市进行过滤:
download
我有一个sts = SpeedTest.joins(:home).where("homes.city": city)
的集合,现在我想按speed_tests
对其进行分组,并获取每个主机名的平均hostname
download
结果如下:
result = sts.select("hostname, AVG(download) as avg_download").group(:hostname)
为什么我看不到列=> #<ActiveRecord::Relation [#<SpeedTest id: nil, hostname: "46.128.35.112.dynamic.cablesurf.de">]>
?我是否可以使用列speed_test ID?我对此属性不感兴趣。
答案 0 :(得分:1)
您看不到它,因为该字段不是模型的一部分,它是虚拟属性。无论如何,它存在。随便叫吧。试试
result.map(&:avg_download)