q: how to list one model in a different model's view form

Hi Guys,

i have a the following models:

job   has_many :job_divisions   has_many :divisions

division   belongs_to :job   has_many :job_divisions

job_division   belongs_to :job   belongs_to :division

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.

Any ideas? Any pointers would be great

Cheers

Adam

job    has_and_belongs_to_many :divisions

division   has_and_belongs_to_many: jobs

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)

here is a source that might help:

its a bit old (rails 1.2) but still aplies

hope this helps

Wolas thankyou very much!

I got solution over at railforum.com as well: http://railsforum.com/viewtopic.php?pid=67294#p67294

Thankyou very much!

I have another question, I would like to use the same setup but this time with a text_field in the form.

The additional field is for a column in the divisions_jobs called 'shotamount'

[code]   <% @job.divisions.each do |d| %>     <p><%= text_field_tag d.shotamount %> <%= d.name %><br/>   <% end %> [/code]

The form loads fine, I can type in numbers to the text_fields, and then hit submit.

However the divisions_jobs.shotamount is not updating in table.

Any ideas?

    <p><%= text_field_tag d.shotamount %> <%= d.name %><br/>

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)

hope it helps

Hi Wolas thanks for continuing to help me out !

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

#JOB VIEW <h1>New job</h1>

<% form_for(@job) do |f| %>   <p>     <b>Job Title</b><br />     <%= f.text_field :job_title %>   </p>

    <b>Division</b><br />

  <% Division.find(:all).each do |d| %>     <%= check_box_tag "job[division_ids]", d.id %> <%= d.name %>   <% end %>

  <p>     <%= f.submit "Create" %>   </p> <% end %>

Ahhh, we have switched methods! the "job[division_ids]" business works for HABTM. so:

what you want to do is attach a division to a job via a checkbox, so the presence of that check box means the division will be added. ok.

<%= check_box_tag "divisions[#{division.name}]", true, job.divisions.include?(division) %>

this will create a params in the form of:

{"divisions" => {'finance' => true, 'it' => 'true'} }

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

hope it helps

even better (refactoring...)

  def update     job = Job.find(params[:id])

    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

hi adam,

i think your problem is that you've declared 'd' as the local variable you're using with the enumeration (when you say, 'each do |d|'.

try changing |d| to |division| and see if that helps.

hth

phantom

Quoting Adam Teale <rails-mailing-list@andreas-s.net>:

Exactly. sorry, i did a quick copy and paste and forgot about that

For the paypal, please, keep it and invite one of your pals to a beer, celebrating the weekend.

glad to help

j

Wolas & Martin thanks again!

The Job.New form now runs without error!!! Cool!!

But, the job_division model (join?) table does not get the job's divisions saved to it.

Any ideas?

Thanks!

Adam