我无法解释为什么如果你直接对关系进行计数而不是映射关系然后计算结果,下面的活动记录关系会返回不同的结果。
他们不应该一样吗?有谁知道发生了什么?
ActiveRecord::Base.connection.query_cache.clear
# => {}
Panel.connection.schema_cache.clear!
# => nil
Panel.reset_column_information
# => nil
@filtered_skew_panels[agglo_code].map(&:panel_id).count
# => 57
@filtered_skew_panels[agglo_code].all.count
(13.1ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "panels" WHERE "panels"."agglo_code_id" = $1 AND "panels"."environment_id" = $2 AND "panels"."product_id" = $3 AND "panels"."alcohol_friendly" = $4 AND "panels"."suburb" = 'Marrickville' AND (NOT EXISTS(SELECT 1 FROM campaign_search_panels WHERE campaign_search_panels.panel_id = panels.panel_id AND campaign_search_panels.campaign_id = 52)) AND (NOT EXISTS(SELECT 1 FROM "AIDAAU_Avails" WHERE "AIDAAU_Avails"."PanelID" = panels.panel_uid AND "AIDAAU_Avails"."TillDate" >= '2017-08-21 00:00:00.000000' AND "AIDAAU_Avails"."FromDate" <= '2017-09-03 00:00:00.000000')) LIMIT 100) subquery_for_count [["agglo_code_id", 4], ["environment_id", 2], ["product_id", 1], ["alcohol_friendly", "t"]]
# => 0
不应该@filtered_skew_panels[agglo_code].map(&:panel_id).count
等于@filtered_skew_panels[agglo_code].count
吗?
关系本身似乎实际上包含所有记录:
[3] pry(#<PanelSearch>)> @filtered_skew_panels[agglo_code]
=> [#<Panel:0x007fe4ef779990
location_type: "Street",
move_id: "27779",
panel_id: "11441A1",
address: "Victoria Rd N/O Sydenham Rd E/S",
postal_code: "2204",
suburb: "Marrickville",
latitude: #<BigDecimal:7fe4ee0b14b0,'-0.33909329E2',18(27
[5] pry(#<PanelSearch>)> @filtered_skew_panels[agglo_code].class
=> Panel::ActiveRecord_Relation
[6] pry(#<PanelSearch>)> @filtered_skew_panels[agglo_code].to_sql
=> "SELECT \"panels\".* FROM \"panels\" WHERE \"panels\".\"agglo_code_id\" = 4 AND \"panels\".\"environment_id\" = 2 AND \"panels\".\"product_id\" = 1 AND \"panels\".\"alcohol_friendly\" = 't' AND \"panels\".\"suburb\" = 'Marrickville' AND (NOT EXISTS(SELECT 1 FROM campaign_search_panels WHERE campaign_search_panels.panel_id = panels.panel_id AND campaign_search_panels.campaign_id = 52)) AND (NOT EXISTS(SELECT 1 FROM \"AIDAAU_Avails\" WHERE \"AIDAAU_Avails\".\"PanelID\" = panels.panel_uid AND \"AIDAAU_Avails\".\"TillDate\" >= '2017-08-21 00:00:00.000000' AND \"AIDAAU_Avails\".\"FromDate\" <= '2017-09-03 00:00:00.000000')) LIMIT 500"
[7] pry(#<PanelSearch>)> @filtered_skew_panels[agglo_code].all.to_sql
=> "SELECT \"panels\".* FROM \"panels\" WHERE \"panels\".\"agglo_code_id\" = 4 AND \"panels\".\"environment_id\" = 2 AND \"panels\".\"product_id\" = 1 AND \"panels\".\"alcohol_friendly\" = 't' AND \"panels\".\"suburb\" = 'Marrickville' AND (NOT EXISTS(SELECT 1 FROM campaign_search_panels WHERE campaign_search_panels.panel_id = panels.panel_id AND campaign_search_panels.campaign_id = 52)) AND (NOT EXISTS(SELECT 1 FROM \"AIDAAU_Avails\" WHERE \"AIDAAU_Avails\".\"PanelID\" = panels.panel_uid AND \"AIDAAU_Avails\".\"TillDate\" >= '2017-08-21 00:00:00.000000' AND \"AIDAAU_Avails\".\"FromDate\" <= '2017-09-03 00:00:00.000000')) LIMIT 500"