Ruby on Rails: How to find all items with a hash that contain a specific value...
Posted
by kingjeffrey
on Stack Overflow
See other posts from Stack Overflow
or by kingjeffrey
Published on 2010-04-25T22:03:37Z
Indexed on
2010/04/25
22:13 UTC
Read the original article
Hit count: 286
Suppose I have three models: Student
, SchoolClass
, and DayOfWeek
. There is a HABTM relationship between Student
and SchoolClass
, and between SchoolClass
and DayOfWeek
. What I'd like to do is find all school classes belonging to a given student that meet on Monday.
Now I suppose I could do something like:
@student = Student.find(:student_id)
@student_classes = @student.school_classes.find(:all)
@student_classes_on_monday = Array.new
@student_classes.each do |student_class|
if student_class.day_of_week.include?("Monday")
@student_classes_on_monday << student_class
end
end
But there has to be a more elegant way. Can you help me find it?
© Stack Overflow or respective owner