Howto implement a join table

Hi There

I have a table for the names of my workers and a table with the
courses they have attended.
Now I try to create a join table to see which workers attended what
courses.

1.) can the join table be done with scaffold
1.1) if yes, does it automatically create the form for assigning
courses to workers
1.2) if no, is there a good(detailed) tutorial for doing this in rails
3

BR
rogi

With Active record , its already link it, i mean, whit @worker.courses
you have a collection of what courses has a worker take it.
With scaffold is to set up custom code to controller/view/model .

On 15 Feb., 11:29, Lorenzo Brito M. [email protected]
wrote:

With Active record , its already link it, i mean, whit @worker.courses
you have a collection of what courses has a worker take it.
With scaffold is to set up custom code to controller/view/model .

I dont now about active record! I mean @worker.course doesn’t work yet
at my project.
I just have the two tables and now i try to easily find a way to get
the html pages, table structures and relations that allow for linking
the courses to the workers?
I’m thinking of a manageWorkersCourses.html that let’s me select a
worker and then add a course from a dropdown list.
On the manageWorkersCourses.html page all the courses of the selected
worker get listed.

BR
rogi

With you create the structure of rails, the model part, has just that
part done for you,
if you want to find relationchips, just go with models, lest say
that you have 3 tables, one for workes and another for courses and the
third that has
one worker and one course,in your manageWorkersCourses.html,
you are goint to brings workers and courses like these @workers =
Worker.all and @courses =Courses.all
then show it in a combobox, then in the form pass to the controller
to create a workercourse,
i reccomend you to follow the main tutorial of rails page, its easy,

On 15 February 2011 10:50, rogi removed_emai[email protected] wrote:

the html pages, table structures and relations that allow for linking
the courses to the workers?
I’m thinking of a manageWorkersCourses.html that let’s me select a
worker and then add a course from a dropdown list.
On the manageWorkersCourses.html page all the courses of the selected
worker get listed.

It seems that maybe you are just starting on Rails and have not yet
acquired some of the basic principles. I suggest you work through the
Rails Guides and some tutorials (try http://railsforzombies.org/ and
railstutorial.org ). Make sure that any tutorial you use is for the
correct major version of Rails (2 or 3). I suggest that you should be
using Rails 3 if you are not already.

Colin

I would do:

  1. rails generate model worker [ definition of model attributes ]
  2. Modify migration file for workers table as needed
  3. Modify app/model/worker.rb to add has_and_belongs_to_many :courses
  4. rails generate model course [ definition of model attributes ]
  5. Modify migration file for course table as needed
  6. Modify app/model/course.rb to add has_and_belongs_to_many :workers
  7. rails generate migration create_course_workers_join_table
  8. Edit this migration to create a new table with the necessary
    foreign
    keys and following the Rails standard naming conventions (plurals of
    each
    table name, in A-Z order).

See
http://guides.rubyonrails.org/association_basics.html#creating-join-tables-for-has_and_belongs_to_many-associations
if
you need something more.

rogi wrote in post #981715:

Hi There

I have a table for the names of my workers and a table with the
courses they have attended.
Now I try to create a join table to see which workers attended what
courses.

Assuming you have completed something like this:
rails g scaffold worker first_name:string last_name:string
rails g scaffold course name:string description:text

(since you seem to be new to Rails, allowing Rails to generate a full
scaffolded solution is nice as an instructional tool - it gives you
something to review to see how the Rails conventions would do the
task… not that anyone leaves the scaffolded solution in place when
actually in production, but I still use it for quick brainstorming
sessions with users who have become accustomed to seeing the “ugly
scaffolded views” when testing out concepts.)

I prefer the has_many through construct, since 99.9% of the time I
always want to store some additional information in the join table, and
having to use the “proper” name for a HABTM join table - is it
worker_course or courses_workers, etc - has always irked me…

Now do:
rails g scaffold training worker_id:integer course_id:integer
start_date:datetime end_date:datetime hours:float
rake db:migrate

in worker.rb, add
has_many :trainings
has_many :courses, :through => :trainings

in course.rb, add
has_many :trainings
has_many :workers, :through => :trainings

in training.rb, add
belongs_to :worker
belongs_to :course

should get you going from the data perspective. A partial displaying the
training for a worker would be good, as would a partial for a course
showing the trainings.

implemented Ar Chrons idea.

Now I wanted to replace the text fields with select boxes.
They look right and let me select values, but the table fields for
worker_id and course_id

don’t get filled: [#<Training id: 1, worker_id: nil, course_id:
nil, …

my form partial _form.html.erb looks like:

<%= select_tag 'worker_id', options_for_select(Worker.all.collect { | w| [w.first_name+" "+w.last_name, w.id] }) %>
<%= select_tag 'course_id', options_for_select(Course.all.collect { |c| [c.name, c.id] }) %>
<%= f.label :start_date %>
<%= f.datetime_select :start_date %>
......

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs