假設我有元素的散列,並且需要根據其中的類名過濾掉結果。如何在軌道上過濾每個模型名稱的記錄數組ruby
因此,如果記錄包含PrivateSchool的元素,我可以將PrivateSchool記錄傳遞到其各自的部分。
如果記錄包含教師的元素,我可以將教師記錄傳遞給其各自的部分。
所以,這裏是partails代碼:
<% if params[:tab] && params[:tab] == "featured" %>
<%= render partial: "featured", locals: {featured: @results} %>
<% elsif params[:tab] && params[:tab] == "schools" %>
<%= render partial: "schools", locals: {schools: @results} %>
<% elsif params[:tab] && params[:tab] == "playlists" %>
<%= render partial: "playlists", locals: {playlists: @results} %>
<% elsif params[:tab] && params[:tab] == "quizzes" %>
<%= render partial: "quizzes", locals: {quizzes: @results} %>
<% elsif params[:tab] && params[:tab] == "teachers" %>
<%= render partial: "teachers", locals: {teachers: @results} %>
<% end %>
如果我做<%= @results.inspect%>
它下面的輸出:
[#<LessonPlan id: 9, title: "advance java", description: "advance java",
notes: "advance java", subject_id: 2, teacher_id: 1, created_at: "2016-11-11
09:29:20", updated_at: "2016-11-11 09:29:20", is_private: true, is_publish:
false, user_id: 3, publish_date: nil, popularity_count: 0, is_special: false,
private_class_id: nil>, #<Quiz id: 1, title: "java history",
time_limit_in_minutes: 5, are_multiple_attempts_allowed: true, score_to_keep:
"highest", number_of_attempts_allowed: 4, student_see_quiz_score:
"after_each_attempt", show_one_question_at_a_time: true, is_private: false,
available_from_date_time: "2016-11-08 17:13:00", available_due_date_time:
"2016-11-10 17:18:00", instructions: "<p>java basics</p>", lesson_plan_id: 3,
user_id: 3, publish_date: "2016-11-09 12:14:04", created_at: "2016-11-09
12:14:04", updated_at: "2016-11-09 12:19:25", is_publish: true, subject_id: 3,
teacher_id: 1, popularity_count: 1, show_answers: "after_the_due_date">,
#<LessonPlan id: 4, title: "basics of java", description: "basics of java",
notes: "basics of java", subject_id: 2, teacher_id: 1, created_at: "2016-11-07
13:04:24", updated_at: "2016-11-10 10:40:54", is_private: false, is_publish:
false, user_id: 3, publish_date: "2016-11-09 08:08:41", popularity_count: 0,
is_special: false, private_class_id: nil>, #<Quiz id: 6, title: "java modern",
time_limit_in_minutes: 155, are_multiple_attempts_allowed: true,
score_to_keep: "latest", number_of_attempts_allowed: 9,
student_see_quiz_score: "after_last_attempt", show_one_question_at_a_time:
true, is_private: true, available_from_date_time: "2017-02-02 01:11:00",
available_due_date_time: "2017-02-27 01:11:00", instructions: "<p>java modern
java modern java modern java modern...", lesson_plan_id: 3, user_id: 3,
publish_date: "2016-12-22 20:37:35", created_at: "2016-12-22 20:37:35",
updated_at: "2017-02-09 20:11:38", is_publish: false, subject_id: 3,
teacher_id: 1, popularity_count: 0, show_answers: "after_the_due_date">,
#<Quiz id: 4, title: "Java quiz v1", time_limit_in_minutes: 180,
are_multiple_attempts_allowed: true, score_to_keep: "latest",
number_of_attempts_allowed: 6, student_see_quiz_score: "after_last_attempt",
show_one_question_at_a_time: false, is_private: false,
available_from_date_time: "2016-11-21 12:48:00", available_due_date_time:
"2016-11-23 12:48:00", instructions: "<p>Java quiz v1</p>\r\n<p
style=\"margin: 0px;\"><!--E...", lesson_plan_id: 8, user_id: 3, publish_date:
"2016-11-14 07:18:41", created_at: "2016-11-14 07:18:41", updated_at:
"2016-12-11 14:21:46", is_publish: false, subject_id: 3, teacher_id: 1,
popularity_count: 0, show_answers: "after_the_quiz">, #<Quiz id: 3, title:
"java basics", time_limit_in_minutes: 4, are_multiple_attempts_allowed: true,
score_to_keep: "highest", number_of_attempts_allowed: 10,
student_see_quiz_score: "after_last_attempt", show_one_question_at_a_time:
true, is_private: false, available_from_date_time: "2017-01-31 14:13:00",
available_due_date_time: "2017-02-17 14:13:00", instructions:
"<p>XXXXXXXXXXQQQQ</p>", lesson_plan_id: 8, user_id: 3, publish_date:
"2016-11-10 20:27:32", created_at: "2016-11-10 20:27:31", updated_at:
"2017-02-01 09:13:22", is_publish: false, subject_id: 3, teacher_id: 1,
popularity_count: 1, show_answers: "after_the_quiz">, #<PrivateSchool id: 18,
teacher_id: 39, user_id: 47, title: "Dr. Jasmine E McNair High School",
short_description: "Dr. Jasmine E McNair High School", price_type: "paid",
logo: "download.jpg", slug: "dr-jasmine-e-mcnair-high-school", created_at:
"2016-12-13 20:18:28", updated_at: "2016-12-13 20:18:28">]
我真正想要的是過濾這些記錄每個模型類的名字,所以我可以過濾記錄並將相應的記錄發送到其分部,而不是僅在每個部分中顯示所有記錄。
目前它顯示在一個部分是錯誤的所有記錄我需要過濾每個部分,因此每個部分將顯示他的結果而不是其他模型。
因此, 如果記錄屬於LessonPlan,則來自LessonPlan的所有記錄將以部分課程計劃進行。
如果記錄屬於教師所有來自教師的記錄將進入教師偏分數。 等。
這是我的要求。
@results.select { |r| r.is_a?(LessonPlan) } # etc.
因此您的視圖可以是這樣的:
<%= render partial: "lesson_plans", locals: {
lesson_plans: @results.select { |r| r.is_a?(LessonPlan) }
} %>
當然@LearningROR – devoh
是的請解釋你的答案,以及這段代碼如何工作,所以它會幫助我和其他未來的讀者。 – LearningROR
@LearningROR它更清楚嗎? – devoh