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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Thu, Dec 13, 2012 at 10:23 PM, Lorenz Blackbird <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> 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. >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.> > Any suggestion? > > Thanks to all. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 13 December 2012 14:23, Lorenz Blackbird <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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.What do you mean by "the same course of exam". Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
> 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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 17 December 2012 08:49, Lorenz Blackbird <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
> Instead of Student.where(:course_id => @exam.course_id) use > @exam.course.studentsGood. Thanks Colin :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.