Rails has_many for sql的子帐户

时间:2019-06-18 07:29:01

标签: ruby-on-rails activerecord has-many find-by-sql

如何在has_many关系中为子记录编写sql?

class Account < ApplicationRecord
  self.primary_key = 'AccountNo'

  has_many :child_accounts, -> { find_by_sql(["SELECT Account.* FROM Account WHERE Account.AccountNo IN (select AccountNo from account_tree where (AccountNo=#{self.AccountNo} OR SubGroupAccountNo=#{self.AccountNo}))"]) }, class_name: "Account"
end

如果我尝试使用account.child_accounts,则表示错误。

NoMethodError: undefined method `extensions' for #<Array:0x007fbb982bbe60>

谢谢

0 个答案:

没有答案