ActiveRecords Associations preloader

27 views
Skip to first unread message

Simioni Tanguy

unread,
Mar 11, 2015, 7:09:47 AM3/11/15
to rubyonra...@googlegroups.com
Hi there ,

i'm trying to preload my associations like this :


@infos = ArchInformation.paginate_by_sql[sql_request], :page =>
params[:page], :per_page => nb_infos_per_page

ActiveRecord::Associations::Preloader.new(@infos,
[:archive_arch_treatments, :archive_arch_contacts]).run

(sql_request containts something like this :

SELECT DISTINCT i.*,u.login, t.dossier_noaors, t.handle_date, c2.email,
Count(fi2.id)
AS nb_fichiers
FROM (((archive.information i
left join archive.treatments t on i.id=t.information_id)
left join public.users u on u.id=t.user_id)
left join archive.contacts c2 on i.id=c2.information_id)
left join archive.fichiers_informations fi2 on i.id=fi2.information_id
left join archive.contents c on c.id = i.content_id
WHERE ((i.is_draft <> 1 AND is_draft <> -1)
OR i.is_draft is null) AND c2.type_contact_id = 1
AND (i.creation_date >= '2015-02-04 00:00:00 +0100' )
GROUP BY i.id, u.login,t.dossier_noaors,t.handle_date,c2.email ORDER BY
creation_date DESC

and i get some result from it


now,here is my models :

class ArchInformation < ActiveRecord::Base
set_table_name 'archive.information'

has_one :archive_arch_treatments, :class_name => 'ArchTreatments'
has_many : archive_arch_contact, : class_name => 'ArchContacts'

.
.
.
end

class ArchContacts < ActiveRecord::Base
set_table_name 'archive.contacts'

.
.
.
end

class Archtreatments < ActiveRecord::Base
set_table°name 'archive.treatments'
belongs_to :archive_arch_information, :class_name =>"ArchInformation",
:foreign_key => "information_id"

.
.
.
end


and when i run this , i've got an error : here is the logs :

PG::UndefinedColumn: ERREUR: la colonne treatments.arch_information_id
n'existe pas
LINE 1: ..."treatments".* FROM "archive"."treatments" WHERE
"archive"....
^
: SELECT "archive"."treatments".* FROM "archive"."treatments" WHERE
"archive"."treatments"."arch_information_id" IN ('1885320', '1885319',
'1885318', '1885317', '1885316', '1885315', '1885314', '1885313')



the error is obvious , when activeRecord build the request , he named my
foreign key "arch_information_id" instead of "information_id" like i
told him in my ArchTreatments class

Why ? And how the fix it ?

Already thanks you

--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Mar 12, 2015, 6:22:00 AM3/12/15
to rubyonra...@googlegroups.com


On Wednesday, March 11, 2015 at 11:09:47 AM UTC, Ruby-Forum.com User wrote:

and when i run this , i've got an error : here is the logs :

PG::UndefinedColumn: ERREUR:  la colonne treatments.arch_information_id
n'existe pas
LINE 1: ..."treatments".* FROM "archive"."treatments"  WHERE
"archive"....
                                                             ^
: SELECT "archive"."treatments".* FROM "archive"."treatments"  WHERE
"archive"."treatments"."arch_information_id" IN ('1885320', '1885319',
'1885318', '1885317', '1885316', '1885315', '1885314', '1885313')



the error is obvious , when activeRecord build the request , he named my
foreign key "arch_information_id" instead of "information_id" like i
told him in my ArchTreatments class


The preload is going from  your ArchInformation to your archive_arch_treatments association - it is the foreign_key setting on this association that is used not the one "on the other side" (active record isn't smart enough to apply options from one to the other)

Fred
Reply all
Reply to author
Forward
0 new messages