Belongs_to optional breaks the display of collection

Hello everyone !

I have the following model :

class Participant
   belongs_to :client, optional: true
end

class Client
  has_many :participants
end

as a participant can

Displaying the collection results in the error " The request to your server either failed or returned invalid data."
Looking at the stacktrace, it seems that the forest_liana tries to order the participant table with the client id, even though it is an optional relationship

Participant Load (0.9ms) SELECT “participants”.* FROM “participants” ORDER BY “clients”.“id” DESC LIMIT $1 OFFSET $2 [[“LIMIT”, 100], [“OFFSET”, 0]]
[2023-05-09 17:49:17] Forest :deciduous_tree::deciduous_tree::deciduous_tree: Records Index error: PG::UndefinedTable: ERROR: missing FROM-clause entry for table “clients”
LINE 1: …ECT “participants”.* FROM “participants” ORDER BY “clients”…

The request sent by the dashboard is the following :

forest/Participant?timezone=America%2FMerida&fields%5BParticipant%5D=id%2Cfirstname%2Clastname&page%5Bnumber%5D=1&page%5Bsize%5D=100&sort=-client.id

I have the following version for the forest_liana gem : 8.0.6 and using rails 6.1.7.3
Thanks for your help

Hello,
I think your collection settings are configured to sort on the client.id field by default. A solution is to change this parameter to another field.
To do this, you can go to the parameters of your collection by following this tutorial.

Spot on, thank you for the quick answer !

1 Like