我有:
class Evaluation < ActiveRecord::Base
has_many :scores
end
class Score < ActiveRecord::Base
scope :for_disability_and_score,
lambda { |disability, score|
where( total_score >= ? AND name = ? , score, disability)
}
end
<代码>分代码> 表格有<条码>总数:条码> 外地和<条码>:条码>外地。
How can i write a scope
to ask for only those evaluations
which have a Score
with name vision and total_score
of 2 and they have another Score
with name hearing and total_score
of 3.
And how can all this be generalized to ask for those evaluations which have n scores with my parameters?
在原材料中,它希望:
I managed to do it in raw sql:
sql = %q-SELECT "evaluations".*
FROM "evaluations"
INNER JOIN "scores" AS s1 ON "s1"."evaluation_id" = "evaluations"."id"
INNER JOIN "target_disabilities" AS t1 ON "t1"."id" = "s1"."target_disability_id"
INNER JOIN "scores" AS s2 ON "s2"."evaluation_id" = "evaluations"."id"
INNER JOIN "target_disabilities" AS t2 ON "t2"."id" = "s2"."target_disability_id"
WHERE "t1"."name" = vision AND (s1.total_score >= 1)
AND "t2"."name" = hearing AND (s2.total_score >= 2)-
这里要重申这一点:
INNER JOIN "scores" AS s1 ON "s1"."evaluation_id" = "evaluations"."id"
(通过将第1条改为第2和第3条等等)
WHERE (s1.total_score >= 1)
但是,它应当成为这样做的铁路...... 希望