如何在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>
谢谢