Database views stop :include directive from working

I’m running rails 2.3.10 on PostgreSQL. I have an issue where if I do
a search on a database view with an :include directrive it does not
load up the associated models as requested so I’m stuck with the old N
+1 problem.

It works fine when dealing with a model from a proper DB table that
includes another model:

BankStatement.all( :include => :bank_account, :limit => 10 )

produces the following in my log file:

SELECT * FROM “bank_statements” ORDER BY statement_date DESC
SELECT * FROM “bank_accounts” WHERE (“bank_accounts”.“id” IN

as expected.

Now in the case where we have objects pulled in from a view
(balace_sheet_transaction is a view that has a statement_account_id):

BalanceSheetTransaction.all( :include => :statement_account, :limit
=> 10 )

the following is in the log

SELECT * FROM “balance_sheet_transactions” LIMIT 10
SELECT * FROM “statement_accounts” WHERE (“statement_accounts”.“id”
= 285)

It only loads up the statement account of the first
balance_sheet_transaction object. If I do an array map to the
statement_account I will then see 10 lines in the log like SELECT *
FROM “statement_accounts” WHERE (“statement_accounts”.“id” = 285)
where each ID is different.

Is there a limitation to using the :include with views? Is it
PostgreSQL specific? Any other hints why :include is being ignored in
the case?

Thanks in advance,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs