views:

45

answers:

1

Hi,

I am trying to populate a ruby on rails select box from a database query, the data comes from 3 tables.

My query

@data = Session.all :include => { :term => :courses }

Object

!ruby/object:Session 
attributes: 
created_at: 2010-06-17 22:12:05
term_id: "15"
updated_at: 2010-06-17 22:12:05
id: "3"
course_id: "1"
attributes_cache: {}

term: &id003 !ruby/object:Term 
attributes: 
  number: "1"
  start_date: 2010-06-17
  created_at: 2010-06-17 22:12:05
  updated_at: 2010-06-17 22:12:05
  id: "15"
attributes_cache: {}

courses: 
- &id001 !ruby/object:Course 
  attributes: 
    created_at: 
    updated_at: 
    course_name: Beginner
    id: "1"
    date: 
    course_type: Programming
  attributes_cache: {}

what i am trying to do is to have the term number followed by the data data and then the course like this

1 01-09-10 Programming Beginners 

The id for the option would be the session_id

any ideas ?

Thanks

Alex

A: 

in your erb template you can put the following code withing you form:

<%= select("session","id", @data.map{|d| ["#{d.term.number} #{d.term.start_date} #{d.course.course_type} #{d.course.course_name}",d.id]} %>
jigfox
Thanks, for your help. when I try and save I get undefined method `reflect_on_association' for Class:ClassMy code looks like this <%= select_tag "contact[session_ids]", options_for_select(@data.map{|d| ["#{d.term.number} #{d.term.start_date} #{d.course.course_type} #{d.course.course_name}",d.id]}) %>thanksAlex
Alex