Salut tout le monde,
Je me demande comment traiter une requete de stat assez complexe de
façon
“propre” en rails.
Voici mes modèles :
Notion de présence au travail
class Attendance < AR::B
belongs_to :user
end
Présence effective (nombre de jours réellement travaillés)
class EffectiveAttendance < Attendance
end
Prévision de présence
class ExpectedAttendance < Attendance
end
class User < AR::B
has_many :permissions
has_many :roles, :through => :permissions
has_many :assignments
has_many :teams, :through => :assignments
belongs_to :job
has_many :attendances
end
class Job < AR::B
has_many :users
belongs_to :occupation
belongs_to :company
end
class Occupation < AR::B
has_many :jobs
end
class Company < AR::B
has_many :jobs
end
class Assignment < AR::B
belongs_to :user
belongs_to :team
end
class Team < AR::B
has_many :assignments
has_many :users, :through => :assignments
end
La requête SQL qui donne ce que je veux :
SELECT c.name AS company_name, o.title AS job_title, j.rate,
SUM(a.am+a.pm)
AS attendances_days_count FROM attendances AS a, users AS u, jobs AS j,
occupations as o, companies AS c, assignments AS ut, teams AS t WHERE
a.user_id = u.id AND u.job_id = j.id AND j.occupation_id = o.id AND
j.company_id = c.id AND u.id = ut.user_id AND ut.team_id = t.id AND t.id
= 1
GROUP BY c.id, o.id;
Sachant que l’idéal serait d’avoir le même traitement pour
EffectiveAttendance que pour ExpectedAttendance (utilisation de la STI)
…
ce n’est pas le cas dans ma requête SQL.
Que dois-je utiliser dans ces cas là ? find_by_sql ? sur quel model ?
Merci
a+
Manu