Forum: Ruby on Rails Filter on multiple select

Posted by Lorenz Blackbird (ratman)
on 2012-12-13 15:23
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.
Posted by Jim ruther Nill (jimboker)
on 2012-12-14 01:37
(Received via mailing list)
On Thu, Dec 13, 2012 at 10:23 PM, Lorenz Blackbird 
<lists@ruby-forum.com>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 rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>


--
Posted by Colin Law (Guest)
on 2012-12-14 22:33
(Received via mailing list)
On 13 December 2012 14:23, Lorenz Blackbird <lists@ruby-forum.com> 
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
Posted by Lorenz Blackbird (ratman)
on 2012-12-17 09:02
> 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 :)
Posted by Lorenz Blackbird (ratman)
on 2012-12-17 09:49
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...
Posted by Colin Law (Guest)
on 2012-12-17 11:02
(Received via mailing list)
On 17 December 2012 08:49, Lorenz Blackbird <lists@ruby-forum.com> 
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
Posted by Lorenz Blackbird (ratman)
on 2012-12-17 12:29
> Instead of Student.where(:course_id => @exam.course_id) use
> @exam.course.students


Good. Thanks Colin :)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.