【问题标题】:jruby rails4 eager_load doesn't workjruby rails4 eager_load 不起作用
【发布时间】:2014-11-06 03:58:15
【问题描述】:

我有两个模型:

class Transaction < ActiveRecord::Base
  self.table_name = 'DEPOSIT_TRANSACTIONS' 
  belongs_to :deposit_account    
  scope :not_test, -> { includes(:deposit_account).where("deposit_accounts.is_test_account is null or deposit_accounts.is_test_account != 1").references(:deposit_account) }
  ...
end

class DepositAccount < ActiveRecord::Base    
  has_many :transactions, -> { order(:creation_date => :desc) }
  default_scope { where(:purpose => 'COUNTERFEIT') }
  scope :not_test, -> { where("is_test_account != 1") }
  ...
end

我尝试使用以下方法加载两个表:

irb(main):008:0> request_transactions = Transaction.eager_load(:deposit_account)
**I polish the following output**
  SQL (6390.0ms)  SELECT "DEPOSIT_TRANSACTIONS"."ID"                    AS t0_r0,
                    "DEPOSIT_TRANSACTIONS"."DEPOSIT_ACCOUNT_ID"         AS t0_r1,
                    "DEPOSIT_TRANSACTIONS"."TRANSACTION_TYPE"           AS t0_r2,
                    "DEPOSIT_TRANSACTIONS"."TRACKING_ID"                AS t0_r3,
                    "DEPOSIT_TRANSACTIONS"."AMOUNT"                     AS t0_r4,
                    "DEPOSIT_TRANSACTIONS"."STATUS"                     AS t0_r5,
                    "DEPOSIT_TRANSACTIONS"."TRANSACTION_COMMENT"        AS t0_r6,
                    "DEPOSIT_TRANSACTIONS"."NOTIFICATION_EMAILS"        AS t0_r7,
                    "DEPOSIT_TRANSACTIONS"."CREATED_BY"                 AS t0_r8,
                    "DEPOSIT_TRANSACTIONS"."CREATION_DATE"              AS t0_r9,
                    "DEPOSIT_TRANSACTIONS"."LAST_UPDATED_BY"            AS t0_r10,
                    "DEPOSIT_TRANSACTIONS"."LAST_UPDATED_DATE"          AS t0_r11,
                    "DEPOSIT_TRANSACTIONS"."FINISH_DATE"                AS t0_r12,
                    "DEPOSIT_TRANSACTIONS"."PAYMENT_DATE"               AS t0_r13,
                    "DEPOSIT_TRANSACTIONS"."MATCH_TYPE"                 AS t0_r14,
                    "DEPOSIT_TRANSACTIONS"."ADDITIONAL_INFO"            AS t0_r15,
                    "DEPOSIT_ACCOUNTS"."ID"                             AS t1_r0,
                    "DEPOSIT_ACCOUNTS"."SELLER_ID"                      AS t1_r1,
                    "DEPOSIT_ACCOUNTS"."MARKETPLACE_ID"                 AS t1_r2,
                    "DEPOSIT_ACCOUNTS"."PURPOSE"                        AS t1_r3,
                    "DEPOSIT_ACCOUNTS"."TOTAL_DEPOSIT_BALANCE"          AS t1_r4,
                    "DEPOSIT_ACCOUNTS"."OUTSTANDING_DEPOSIT_REQUEST"    AS t1_r5,
                    "DEPOSIT_ACCOUNTS"."OUTSTANDING_REFUND_REQUEST"     AS t1_r6,
                    "DEPOSIT_ACCOUNTS"."CREATED_BY"                     AS t1_r7,
                    "DEPOSIT_ACCOUNTS"."CREATION_DATE"                  AS t1_r8,
                    "DEPOSIT_ACCOUNTS"."LAST_UPDATED_BY"                AS t1_r9,
                    "DEPOSIT_ACCOUNTS"."LAST_UPDATED_DATE"              AS t1_r10,
                    "DEPOSIT_ACCOUNTS"."IS_TEST_ACCOUNT"                AS t1_r11,
                    "DEPOSIT_ACCOUNTS"."RECORD_VERSION_NUMBER"          AS t1_r12,
                    "DEPOSIT_ACCOUNTS"."OUTSTANDING_CONFISCATE_REQUEST" AS t1_r13
                  FROM "DEPOSIT_TRANSACTIONS"
                  LEFT OUTER JOIN "DEPOSIT_ACCOUNTS"
                  ON "DEPOSIT_ACCOUNTS"."ID"       = "DEPOSIT_TRANSACTIONS"."DEPOSIT_ACCOUNT_ID"
                  AND "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT'

  DepositAccount Load (235.0ms)  SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 143 AND ROWNUM <= 1
  DepositAccount Load (535.0ms)  SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 143 AND ROWNUM <= 1
  DepositAccount Load (471.0ms)  SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 147 AND ROWNUM <= 1
  DepositAccount Load (237.0ms)  SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 138 AND ROWNUM <= 1
  ...

但它会生成这么多 SQL,这意味着我认为 rails 不会急切地加载。我预计第一个条款就足够了。请帮助急切加载表格。谢谢。

我的环境:
jruby 1.7
轨道 4.0

EDIT1:
我试图在 ruby​​ 1.9 rails 4.0 上运行相同的代码,急切的负载工作正常。所以我猜这是由 JRuby 引起的。其他人有这个问题吗?

不知道是否重要,我在 ruby​​ 1.9 环境中使用 activerecord-oracle_enhanced-adapter,但在 JDBC 驱动程序中 杰鲁比。它是 oracle 的数据库。

【问题讨论】:

  • 我怀疑生成的 SQL 不是由 Transaction.eager_load(:deposit_account) 生成的,或者您的 Transaction 模型缺少某些内容。原因是生成的连接条件。这个:AND "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' 来自哪里?
  • @tobago "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' 来自 default_scope { where(: purpose => 'BOOK') },我在发布问题时犯了一个错误,应该是default_scope { where(: purpose => 'COUNTERFEIT') }.
  • 我仍然怀疑生成的 SQL 不是由 Transaction.eager_load(:deposit_account) 生成的,或者您的 Transaction 模型缺少某些内容。 belongs_to: :deposit_account 单独永远不会生成组合连接条件,例如:ON "DEPOSIT_ACCOUNTS"."ID" = "DEPOSIT_TRANSACTIONS"."DEPOSIT_ACCOUNT_ID" AND "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT'
  • @tobago SQL 完全由模型和 Transaction.eager_load(:deposit_account) 生成。 where 子句由default_scope 生成。

标签: ruby-on-rails jruby jrubyonrails


【解决方案1】:

通过将activerecord从activerecord-4.0.9升级到activerecord-4.1.5解决了这个问题。顺便说一句,我使用 activerecord-jdbc-adapter-1.3.7 作为适配器。

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 2013-01-11
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多