Hello everyone,
this is the first time I''ve built a Rails application that has a
many-to-many relationship, and I''m having a bit of trouble with a
method.
The application is one where computer technician can enter jobs, update
them, etc. I have a list of jobs already (entered manually with CocoaMySQL)
and now I need to update a job with Rails. A technician can have many jobs
and a job can have many technicians, so I made the appropriate middle table.
I''m gonna post some code to help you help me.
The model code:
class Job < ActiveRecord::Base
belongs_to :category
belongs_to :priority
has_and_belongs_to_many :technician
end
class Technician < ActiveRecord::Base
has_and_belongs_to_many :job
def full_name
last_name + ", " + first_name
end
end
The code in my controller looks like this:
# Save a modified job
def save
job = Job.find(@params["job"]["id"])
job.attributes = @params["job"]
job.technician = Technician.find(@params["techs"])
if job.save
redirect_to "list"
else
render_text "Error!"
end
end
This doesn''t err, but when I go back to the list, if I added or removed
a
technician from a job, it is not taken into account, the thing is just like
before. I tried it manually with script/console and the job goes back to its
old technicians after I call job.save.
Thanks for your help,
Vincent.
--
Vincent Foley-Bourgon
Blog: http://www.livejournal.com/~gnuvince
World.run while (6 * 9 == 42)
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails