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
--
Posted via http://www.ruby-forum.com/.
I got solution over at railforum.com as well:
http://railsforum.com/viewtopic.php?pid=67294#p67294
Thankyou very much!
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?
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 %>
Thankyou so much for your efforts to explain all this.
I have used your code but rails seems to not know about the variable
'division'
:) If you have a paypal account I will happily drop some money in it!
I am so brain dead after looking at this all day!
Thanks!
Adam
#################################
NameError in Jobs#new
Showing jobs/new.html.erb where line #16 raised:
undefined local variable or method `division' for
#<ActionView::Base:0x2624f24>
Extracted source (around line #16):
13: <b>Division</b><br />
14:
15: <% Division.find(:all).each do |d| %>
16: <%= check_box_tag "divisions[#{division.name}]", true,
job.divisions.include?(division) %>
17: <% end %>
18:
19: <p>
RAILS_ROOT: /Users/ateale/rails/frameworx
Application Trace | Framework Trace | Full Trace
app/views/jobs/new.html.erb:16:in
`_run_erb_47app47views47jobs47new46html46erb'
app/views/jobs/new.html.erb:15:in `each'
app/views/jobs/new.html.erb:15:in
`_run_erb_47app47views47jobs47new46html46erb'
app/views/jobs/new.html.erb:7:in
`_run_erb_47app47views47jobs47new46html46erb'
app/controllers/jobs_controller.rb:40:in `new'
Request
Parameters:
None
Show session dump
Response
Headers:
{"cookie"=>[],
"Content-Type"=>"text/html",
"Cache-Control"=>"no-cache"}
#################################
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-mai...@andreas-s.net>:
--
-------------------------------------------------
Visit Pipex Business: The homepage for UK Small Businesses
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