# $50 to anyone - 1 form - multiple models, HMT w/check\_boxes

**URL:** <https://www.ruby-forum.com/t/50-to-anyone-1-form-multiple-models-hmt-w-check-boxes/142454>\
**Category:** Rails\
**Created:** [July 8, 2008, 4:47am UTC](https://www.ruby-forum.com/t/50-to-anyone-1-form-multiple-models-hmt-w-check-boxes/142454 "2008-07-08T04:47:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 8, 2008, 4:47am UTC](https://www.ruby-forum.com/t/50-to-anyone-1-form-multiple-models-hmt-w-check-boxes/142454/1 "2008-07-08T04:47:00Z")

</div>

Hi Guys,

Firstly apologies for posting again.

I have been through a load of headache and days deep in google trying to  
find an example/solution to my problem.  
A lot of good people have been helping out a lot but I still haven’t  
managed to knock it over yet.

So, I am offering $50USD (via paypal) to anyone who can get this working  
for me. Or I can send you something cool from Thailand (where I live)

I have the following models in a has\_many :through relationship.  
Job, Division, JobDivisions.

Each job can involve any number of divisions.

The 3 things I need to do:

1. When I create a new Job I want to be able to select the appropriate  
divisions using checkboxes.
2. I guess I will probably need to edit a job later - add/remove  
divisions
3. The other step in ‘setting up a job’ is I have a script that builds  
a bunch of different directories depending on the divisions the job has.  
This, hopefully, will get information from the JobDivisions JOIN table -  
basically ‘how many directories for each division to build’.

Most importantly I need to work out point (1).

Maybe the only problem is that I have no idea how to use the checkbox’s  
output to create new JobDivisions.

NOTES:  
I am using form\_for, but am wondering if I should be using form\_for\_tag  
My tables seem to be ok - the JobDivision table has job\_id:integer,  
division\_id:integer & one called shotamount:integer - for point (3)  
above.

Please help!

Very Sincerely!, Adam

As for errors I currently get:

* * *

NoMethodError in JobsController#create

undefined method `values’ for “true”:String  
RAILS\_ROOT: /Users/ateale/frameworx

Application Trace | Framework Trace | Full Trace  
app/controllers/jobs\_controller.rb:72:in `create' app/controllers/jobs_controller.rb:71:in`each\_value’  
app/controllers/jobs\_controller.rb:71:in `create’  
Request

Parameters:

{“commit”=\>“Create”,  
“authenticity\_token”=\>“c37334c050f07baa76c1daa9fcf08f0416902ce3”,  
“job”=\>{“job\_title”=\>“green”},  
“divisions”=\>{“1”=\>“true”,  
“3”=\>“true”}}  
Show session dump

Response

Headers:

## {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

Here are my files so far:

##############  
MODELS  
##############

* * *

## job.rb

class Job \< ActiveRecord::Base  
has\_many :job\_divisions  
has\_many :divisions, :through =\> :job\_divisions  
end

* * *

## division.rb

class Division \< ActiveRecord::Base  
has\_many :job\_divisions  
has\_many :jobs, :through =\> :job\_divisions  
end

* * *

## job\_division.rb

class JobDivision \< ActiveRecord::Base  
belongs\_to :job  
belongs\_to :division  
end

##############  
VIEWS  
##############

* * *

## new.html.erb

# New job

\<%= error\_messages\_for :job %\>

\<% form\_for(@job) do |f| %\>

**Job Title**  
 \<%= f.text\_field :job\_title %\>

\<% Division.find(:all).each do |division| %\>

\<%= check\_box\_tag “divisions[#{division.id}]”, true,  
@job.divisions.include?(division.id) %\> \<%= division.name %\>

  
\<% end %\>

\<%= f.submit "Create" %\>

\<% end %\>

##############  
CONTROLLERS  
##############

* * *

## jobs\_controller.rb

def new  
@job = Job.new

Division.find(:all).each do |c|  
@job.job\_divisions.build(:id =\> c)  
end

respond\_to do |format|  
format.html # new.html.erb  
format.xml { render :xml =\> @job }  
end  
end

def create  
@job = Job.new(params[:job])  
@job.user\_id = session[:user\_id]

params[:divisions].each\_value do |k|  
@job.job\_divisions.build(k)  
end

respond\_to do |format|  
if @job.save  
flash[:notice] = ‘Job was successfully created.’  
format.html { redirect\_to(@job) }  
format.xml { render :xml =\> @job, :status =\> :created, :location  
=\> @job }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @job.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

def update

params[:post][:tag\_ids] ||= []  
@job = Job.find(params[:id])

if @job.update\_attributes(params[:job])  
flash[:notice] = ‘Post was successfully updated.’  
else  
end

respond\_to do |format|  
if @job.update\_attributes(params[:job])  
flash[:notice] = ‘Job was successfully updated.’  
format.html { redirect\_to(@job) }  
format.xml { head :ok }  
else  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @job.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 8, 2008, 5:12am UTC](https://www.ruby-forum.com/t/50-to-anyone-1-form-multiple-models-hmt-w-check-boxes/142454/2 "2008-07-08T05:12:13Z")

</div>

Adam –

Is it feasible to use something like active scaffold?

> **[噂のUR賃貸事情 |](http://www.activescaffold.com/)**
>
> UR賃貸住宅は、子連れ家族に人気があります。 1つは、間取りに対する専有面積が広いところです。将来的に家族が増えても安心ですよ。次に、安全性の高い子供達の共有空間がある点です。最後は、緑溢れる豊かな自然環境があるところです。子供達がのびのびと成長していける空間が、UR賃貸住宅には沢山あります。

If so, that’s the easiest way I know to solve this. If not, I’d still  
be happy to help.

Matt

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 8, 2008, 5:39am UTC](https://www.ruby-forum.com/t/50-to-anyone-1-form-multiple-models-hmt-w-check-boxes/142454/3 "2008-07-08T05:39:48Z")

</div>

hey Matt thanks for reading my post

I’ll check that out. I was hoping to try build most of the app - from  
what I had seen on the web most of this seemed built into rails.

Thanks for the link!

Adam

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 8, 2008, 10:45am UTC](https://www.ruby-forum.com/t/50-to-anyone-1-form-multiple-models-hmt-w-check-boxes/142454/4 "2008-07-08T10:45:40Z")

</div>

I was trying to do something similar and found a much better approach  
than using checkboxes, which was ugly in code and UI. Check out these  
Railscasts, just following these through gave me a good start:

> **[\#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[\#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74-complex-forms-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[\#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75-complex-forms-part-3)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

On Jul 7, 7:47 pm, Adam T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 8, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/50-to-anyone-1-form-multiple-models-hmt-w-check-boxes/142454/5 "2008-07-08T17:04:32Z")

</div>

Adam –

In your controller’s create, you have:

@job = Job.new(params[:job])  
@job.user\_id = session[:user\_id]

params[:divisions].each\_value do |k|  
@job.job\_divisions.build(k)  
end

The problem is that it hasn’t created an id for the job yet, so it can’t  
define the relationship between the job and the division.

You need to save the job prior to adding the divisions.

@job.save!

Hope this helps,  
Matt

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [July 8, 2008, 9:43am UTC](https://www.ruby-forum.com/t/50-to-anyone-1-form-multiple-models-hmt-w-check-boxes/142454/6 "2008-07-08T09:43:20Z")

</div>

I think i might be getting closer!

But I am getting this error (does it mean it is trying to save the Join  
table before it has created a Job? and therefore can’t reference a  
job\_id?) :

## THE ERROR:

## “You cannot call create unless the parent is saved”

Any ideas how to save the parent?

## my updated Job.rb 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=(division\_ids)  
job\_divisions.each do |job\_divi|  
# destroying the item you are enumerating over usually isn’t a  
good idea.  
# So this may not work:  
job\_divi.destroy unless division\_ids.include? job\_divi.division\_id  
end

```
division_ids.each do |division_id|
  self.job_divisions.create(:division_id => division_id) unless 

```

job\_divisions.any? { |d| d.division\_id == division\_id }  
end  
end

## end

## updated new.html.erb view:

# New job

\<%= error\_messages\_for :job %\>

\<% form\_for(@job) do |f| %\>

**Job Title**  
 \<%= text\_field :job, :job\_title %\>

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

```

\<% for division in Division.find(:all) %\>

 \<%= check\_box\_tag "job[division\_ids][]", division.id, @job.job\_divisions.include?(division) %\>\<%= division.name%\> 
 \<% end %\> 

\<%= f.submit "Create" %\>

\<% end %\>
## \<%= link\_to ‘Back’, jobs\_path %\>
