Filter on multiple select

Hello,

I have a many-to-many relationship between the model “exams” and the
model “students”, in the middle I have the model “registrations”.

Students are registered to a “course” (another model) and any exam is
associated to a “course”.

I create a view in exams that permit to add the students with multiple
selection:

<%= f.select :student_ids, Student.all.map { |student| [student.name,
student.id] }, { }, { multiple: true }

but this solution show me all students in the db. I wish I had only the
students registered in the same course of exam.

I don’t know how filter the array and put it in the multiple selection.

Any suggestion?

Thanks to all.

On Thu, Dec 13, 2012 at 10:23 PM, Lorenz B.
[email protected]wrote:

<%= f.select :student_ids, Student.all.map { |student| [student.name,
student.id] }, { }, { multiple: true }

but this solution show me all students in the db. I wish I had only the
students registered in the same course of exam.

I don’t know how filter the array and put it in the multiple selection.

I think you need to add a relationship between a course and a user and
use
that relationship to figure out that list of student choices eligible
for
the exam.

“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.

On 13 December 2012 14:23, Lorenz B. [email protected]
wrote:

<%= f.select :student_ids, Student.all.map { |student| [student.name,
student.id] }, { }, { multiple: true }

but this solution show me all students in the db. I wish I had only the
students registered in the same course of exam.

What do you mean by “the same course of exam”.

Colin

I think you need to add a relationship between a course and a user and
use that relationship to figure out that list of student choices eligible
for the exam.

In this project, any student can join only one course. In fact, the
relationship between students and course is:

belongs_to :course # in student.rb
has_many :students # in course.rb

The DB have 4 table:

Students (n)---------(1) Courses
(1) (1)
| |
| |
(n) (n)
Registrations (n)-----(1) Exams

I create 2 views to manage Exams table. One, to insert title, date and
course_id; another one to join stundents and an existed exam. In this
second view I wish to present to a user (in a multiple selection, or
something like that) only the students associated to a course_id of
exam.

To populate the Registrations table, I use a multiple selection, but I’m
not able to filter the students.

What do you mean by “the same course of exam”.

I hope the graph help to understand… and I hope my english isn’t so
bad :slight_smile:

On 17 December 2012 08:49, Lorenz B. [email protected]
wrote:

Sorry for the double post.

I try to use squeel gem in this way:

<%= f.select :students_ids, Student.where(:course_id =>
@exam.course_id).map { |student| [student.name, student.id] }, { }, {
multiple: true } %>

Instead of Student.where(:course_id => @exam.course_id) use
@exam.course.students

Also I don’t see where the squeel gem is relevant to the code you
showed.

Colin

Instead of Student.where(:course_id => @exam.course_id) use
@exam.course.students

Good. Thanks Colin :slight_smile:

Sorry for the double post.

I try to use squeel gem in this way:

<%= f.select :students_ids, Student.where(:course_id =>
@exam.course_id).map { |student| [student.name, student.id] }, { }, {
multiple: true } %>

I replaced .all with .where(condition). After two days of tests I find
this solution and seems works.

Is it correct? I started to use Rails three weeks ago and I’m not
sure…