Find :include broken when upgrading to 2.2.2


#1

Hi, I’m upgrading an old 1.2 application to latest 2.2 and I got an
error which is certainly due to :include

@factures = Facture.find(:all, :include => [[:forfait => :vehicule] =>
:client], :conditions => [“MONTH(factures.date_emission) = ? AND
YEAR(factures.date_emission) = ?”, @mois, @annee], :order =>
‘clients.nom’)

This code produces an error

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.macro

Does the :include option have changed its behaviour?

However the above line of code generate a valid SQL statement, run it
but fails after.

SELECT factures.id AS t0_r0, factures.forfait_id AS t0_r1,
factures.titre AS t0_r2, factures.date_emission AS t0_r3,
factures.date_envoyee AS t0_r4, factures.date_payee AS t0_r5,
forfaits.id AS t1_r0, forfaits.vehicule_id AS t1_r1,
forfaits.genre AS t1_r2, forfaits.periodicite_reglement AS
t1_r3, forfaits.date_debut AS t1_r4, forfaits.date_fin AS t1_r5,
forfaits.en_cours AS t1_r6, vehicules.id AS t2_r0,
vehicules.client_id AS t2_r1, vehicules.immatriculation AS
t2_r2, vehicules.marque AS t2_r3, vehicules.modele AS t2_r4,
vehicules.veh_type AS t2_r5, vehicules.num_serie AS t2_r6,
vehicules.couleur AS t2_r7, vehicules.puissance AS t2_r8,
vehicules.date_pmec AS t2_r9, vehicules.date_ct AS t2_r10,
vehicules.active AS t2_r11, vehicules.created_at AS t2_r12,
vehicules.notes AS t2_r13, vehicules.emplacement AS t2_r14,
clients.id AS t3_r0, clients.nom AS t3_r1, clients.prenom AS
t3_r2, clients.adresse AS t3_r3, clients.email AS t3_r4,
clients.created_at AS t3_r5, clients.notes ASt3_r6,
clients.periodicite_reglement AS t3_r7 FROM factures LEFT OUTER
JOIN forfaits ON forfaits.id = factures.forfait_id LEFT OUTER JOIN
vehicules ON vehicules.id = forfaits.vehicule_id LEFT OUTER JOIN
clients ON clients.id = vehicules.client_id WHERE
(MONTH(factures.date_emission) = 6 AND YEAR(factures.date_emission) =
2009) ORDER BY clients.nom

Any help appreciated, thanks


#2

On Jun 4, 1:19 pm, Bob M. removed_email_address@domain.invalid
wrote:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.macro

Does the :include option have changed its behaviour?

It did change slightly. I suspect that you originally got lucky: your
include clause is

[[:forfait => :vehicule] => :client]

which ruby parses as
[{[{:forfait=>:vehicule}]=>:client}]

which isn’'t the type of structure that rails is expecting (but
obviously happened to work in the past). It looks like what you should
be saying is

{:forfait => {:vehicule => :client}}

Fred


#3

Frederick C. wrote:

On Jun 4, 1:19�pm, Bob M. removed_email_address@domain.invalid
wrote:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.macro

Does the :include option have changed its behaviour?

It did change slightly. I suspect that you originally got lucky: your
include clause is

[[:forfait => :vehicule] => :client]

which ruby parses as
[{[{:forfait=>:vehicule}]=>:client}]

which isn’'t the type of structure that rails is expecting (but
obviously happened to work in the past). It looks like what you should
be saying is

{:forfait => {:vehicule => :client}}

Fred

Thanks a lot Fred ! It’s always sensible when somebody gives you the
solution :wink:
Have a nice day