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
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 .
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.
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,
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.
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).
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.