When I do a Job.new I would like to have a list of all the divisions,
and be able to select them with checkboxes.
Once the Job.create is run a job_division.create is run for each of the
selected divisions in the above list.
Something like that.
I can't work out how to get the list of Divisions to appear in the View
for the Job.new, and then how to get it to save all the info.
divisions_jobs (its a many to many join table, rails conventions make
it alphabetical order and plurars)
has no model but in the table should be
job_id:integer
division_id:integer
then you can do in the view
<%= check_box_tag "job[division_ids]", division.id,
@job.divisions.include?(division) %>
<%= division.name %>
Remember that in HTML, when checkboxes are not checked, they DONT
appear in params at all (as opposed to appeaaring as :name => false)
1) You have the shotamount pointing to the divisions table, not the
joined table. (d.shotamount? then the model division has a column
shotamount or a method shotamount?)
2) The text_field_tag gives you a plain text field. the controller
will not use the value at all to do mass assignment (what it does to
update_attributes or Model.new(:foo => 'bar' ).
3) if you look at the api: ActionView::Helpers::FormTagHelper
you have put the wrong arguments in the wrong places. Ruby will let
you do this (its not like java, and rails will do even more auto-magik
(accept integers as strings and vice versa) ) so always always read
the api of a function if you are unsure.
4) in HABTM you cant have rich associations. ie: no more attributes
than 2 (maybe more) ids and thats it. if you need rich associations,
you ill have to use has_many :through. there are plenty of blogs about
i recommend Josh suser (i think its spelt like this)
I am now trying to do this with "has_many :through" as you suggested but
have been losing hair trying to work this out. I don't really get it,
and the frustration of not understanding is killing me!
Would you be able to have a look again?
Thanks if you can!
Adam
#JOB MODEL
class Job < ActiveRecord::Base
has_many :job_divisions
has_many :divisions, :through => :job_divisions
def self.options_for_select
self.find(:all).collect{|job| [job.job_title]}
end
def division_ids(divids=)
divids.each do |divid|
job_divisions.build(:division_id => divid)
end
end
end
#DIVISION MODEL
class Division < ActiveRecord::Base
has_many :job_divisions
has_many :jobs, :through => :job_divisions
end
#JOB_DIVISION MODEL
class JobDivision < ActiveRecord::Base
belongs_to :job
belongs_to :division
end
this is not understood by rails by default. so we have to work a bit
in the controller. (most important thing is to remember that if the
check box is unchecked it wil NOT give 'name' => false. it will just
not appear at all)
def update
job = Job.find(params[:id])
# here is the beauty of ruby...whatever gets returned by the if
# block gets assigned to jobs-new-divisions
jobs_new_divisions = if params[:divisions]
# we only want the name because presence means it was checked
params[:divisions].map{|key, value| key }
else
# just in case we have everything unchecked
end
if job.update_attributes(params[:job])
Job.divisions = jobs_new_divisions
Job.save
flash[:notice] = "Roles for #{player.name} succesfully updated"
redirect_to :action => 'index'
else
# bla bla bla
end
end
if job.update_attributes(params[:job])
# here is the beauty of ruby...whatever gets returned by the if
# block gets assigned to jobs-new-divisions
jobs.divisions = if params[:divisions]
# we only want the name because presence means it was checked
params[:divisions].map{|key, value| key }
else
# just in case we have everything unchecked
end
Job.save! #bang because we should be certain that everything is
ok.
flash[:notice] = "#{job.name} succesfully updated"
redirect_to :action => 'index'
else
# bla bla bla
end
end