# N:m relationship, joining table record creation issue

**URL:** <https://www.ruby-forum.com/t/n-m-relationship-joining-table-record-creation-issue/166947>\
**Category:** Rails\
**Created:** [May 1, 2009, 9:42am UTC](https://www.ruby-forum.com/t/n-m-relationship-joining-table-record-creation-issue/166947 "2009-05-01T09:42:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahad\_LSAmdani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7feea3/32.png) [@Ahad\_LSAmdani](https://www.ruby-forum.com/u/Ahad_LSAmdani)\
**Post date:** [May 1, 2009, 9:42am UTC](https://www.ruby-forum.com/t/n-m-relationship-joining-table-record-creation-issue/166947/1 "2009-05-01T09:42:05Z")

</div>

Hey all,

I have an n:m relationship between lpl\_short\_form\_apps and  
practice\_areas. The joining table is called practices. The definitions  
are as follows (minus validation/business logic):

LPLSFAs Model:  
class LPLShortFormApp \< ActiveRecord::Base  
has\_many :practices  
has\_many :practice\_areas, :through =\> :practices  
has\_many :attorneys  
end  
Table:  
class CreateLplShortFormApps \< ActiveRecord::Migration  
def self.up  
create\_table :lpl\_short\_form\_apps do |t|  
t.string :applicant\_name, :null =\> false  
t.string :principal\_business\_address  
…  
t.string :email\_address, :null =\> false  
t.datetime :effective\_date\_requested  
…  
t.timestamps  
end  
end

def self.down  
drop\_table :lpl\_short\_form\_apps  
end  
end

Practice Areas Model:  
class PracticeArea \< ActiveRecord::Base  
has\_many :practices  
has\_many :lpl\_short\_form\_apps, :through =\> :practices  
end  
Table:  
class CreatePracticeAreas \< ActiveRecord::Migration  
def self.up  
create\_table :practice\_areas do |t|  
t.string :area  
t.integer :specialty

```
  t.timestamps
end

```

end

def self.down  
drop\_table :practice\_areas  
end  
end

Model:  
class Practice \< ActiveRecord::Base  
belongs\_to :lpl\_short\_form\_app, :class\_name =\> “LPLShortFormApp”  
belongs\_to :practice\_areas  
end  
Table:  
class Practices \< ActiveRecord::Migration  
def self.up  
create\_table :practices, :id =\> false do |t|  
t.integer :lplsfa\_id, :null =\> false, :options =\> “CONSTRAINT  
fk\_practices\_lplsfas REFERENCES lpl\_short\_form\_apps(id)”  
t.integer :practice\_area\_id, :null =\> false, :options =\>  
“CONSTRAINT fk\_practices\_areas\_of\_practice REFERENCES practice\_area  
(id)”

```
  t.decimal :percentage, :null => false
end

```

end

def self.down  
drop\_table :practices  
end  
end

I think that’s the proper way to setup the relationships. If it isn’t,  
can someone guide me along the right method?

What I want to do, what I’m having an issue doing, is generating the  
new records in the n:m relationship. By business rules, we have 30  
Practice Areas set up. Each LPLSFA can have a percentage of those  
areas up to 100%; so, I have a practices\_controller defined with an  
index that generates a form with all of the areas of practice and a  
percentage field.

This is the controller code:  
def index  
@lpl\_short\_form\_app = LPLShortFormApp.find(session  
[:lplsfa\_id])  
@practice = Practice.new  
@practice\_areas = PracticeArea.all  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @practice }  
end  
end

This is the index page:

 \<% form\_for(@practice) do |f| %\> \<%= f.error\_messages %\> Listing all Areas of Practice for \<%= @lpl\_short\_form\_app.applicant\_name %\> \<% @practice\_areas.each do |practice\_area| %\> 
 \<%= f.label practice\_area.area %\> \<%= f.text\_field :percentage, :id =\> practice\_area.area %\> 
 \<% end %\> 
 \<%= f.submit 'Set' %\> 
 \<% end %\>

So, my questions are as follows:

1. How do I have the submit button go to the “Set” action defined in  
the practices\_controller?
2. How do I collect/scrape each of the individual percentages from the  
form so that I can validate them against a total of 100%?
3. How do I take that collection and generate a new record in the  
practices table such that it joins the LPLSFA with the Practice Area?  
Each LPLSFA will have all 30 practice areas, so 30 practices records,  
most of which will probably be at a 0 percentage. I’m imagining  
something like this:

def set  
@practice\_areas = PracticeArea.all  
@lpl\_short\_form\_app = LPLShortFormApp.find(session[:lplsfa\_id])  
@practice\_areas.each do |practice\_area|  
@practice\_areas.lpl\_short\_form\_apps \<\< @lpl\_short\_form\_app  
end  
end

I know this is a long message, but I hope it gets a fair evaluation.  
It breaks down to my lack of understanding of many to many  
relationships in Ruby, my lack of understanding of the form\_for in  
Rails, and my lack of understanding of both ruby/rails general syntax.  
I have the Ruby 1.9 Pickaxe book as well as the Agile Web Dev with  
Rail, 3rd ed. but having read through the beginning sections of both  
books hasn’t cleared this up for me.

---

<div class="post-metadata">

**Author:** ![Ahad\_LSAmdani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7feea3/32.png) [@Ahad\_LSAmdani](https://www.ruby-forum.com/u/Ahad_LSAmdani)\
**Post date:** [May 1, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/n-m-relationship-joining-table-record-creation-issue/166947/2 "2009-05-01T12:54:51Z")

</div>

On Apr 30, 10:24 pm, “Ahad L. Amdani” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So, my questions are as follows:
> 
> 1. How do I have the submit button go to the “Set” action defined in  
> the practices\_controller?

by default this form would submit to the update (if the record  
existed) or create (if it didn’t) url for that practice - which  
doesn;t sound quite right - the model you are editing  
is LPLShortFormApp as I understand it (since the list of areas is a  
fixed list). if what you are doing feels like an update/edit/create i  
would stick with the default set of actions rather than create a set  
action

> 1. How do I collect/scrape each of the individual percentages from the  
> form so that I can validate them against a total of 100%?

you’re going to have to plough through the params hash. right now it  
will be bust since all of your form inputs have the same name (and so  
will overwrite each other). The form helpers guide (on  
[guides.rubyonrails.org](http://guides.rubyonrails.org)) has information on dealing with that sort of  
thing.

> 1. How do I take that collection and generate a new record in the  
> practices table such that it joins the LPLSFA with the Practice Area?  
> Each LPLSFA will have all 30 practice areas, so 30 practices records,  
> most of which will probably be at a 0 percentage. I’m imagining  
> something like this:

you’re starting to get into the complex forms arena (ie a form that  
does not edit/update a single row in the database). one way of going  
it would be to have a method on LPLShortFormApp that took as an  
argument the info from the params and created the rows in the join  
table.

Fred

---

<div class="post-metadata">

**Author:** ![Ahad\_LSAmdani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7feea3/32.png) [@Ahad\_LSAmdani](https://www.ruby-forum.com/u/Ahad_LSAmdani)\
**Post date:** [May 4, 2009, 8:49am UTC](https://www.ruby-forum.com/t/n-m-relationship-joining-table-record-creation-issue/166947/3 "2009-05-04T08:49:42Z")

</div>

Frederick C. wrote:

> On Apr 30, 10:24ï¿½pm, “Ahad L. Amdani” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > So, my questions are as follows:
> > 
> > 1. How do I have the submit button go to the “Set” action defined in  
> > the practices\_controller?
> 
> by default this form would submit to the update (if the record  
> existed) or create (if it didn’t) url for that practice - which  
> doesn;t sound quite right - the model you are editing  
> is LPLShortFormApp as I understand it (since the list of areas is a  
> fixed list). if what you are doing feels like an update/edit/create i  
> would stick with the default set of actions rather than create a set  
> action
> 
> > 1. How do I collect/scrape each of the individual percentages from the  
> > form so that I can validate them against a total of 100%?
> 
> you’re going to have to plough through the params hash. right now it  
> will be bust since all of your form inputs have the same name (and so  
> will overwrite each other). The form helpers guide (on  
> [guides.rubyonrails.org](http://guides.rubyonrails.org)) has information on dealing with that sort of  
> thing.
> 
> > 1. How do I take that collection and generate a new record in the  
> > practices table such that it joins the LPLSFA with the Practice Area?  
> > Each LPLSFA will have all 30 practice areas, so 30 practices records,  
> > most of which will probably be at a 0 percentage. I’m imagining  
> > something like this:
> 
> you’re starting to get into the complex forms arena (ie a form that  
> does not edit/update a single row in the database). one way of going  
> it would be to have a method on LPLShortFormApp that took as an  
> argument the info from the params and created the rows in the join  
> table.
> 
> Fred

Thanks for the assistance. I’ve been looking at the guides, and I found  
this in 7.3 of the forms guide:

\<% form\_for @person do |person\_form| %\>  
\<%= person\_form.text\_field :name %\>  
\<% for address in @person.addresses %\>  
\<% person\_form.fields\_for address, :index =\> address do  
|address\_form|%\>  
\<%= address\_form.text\_field :city %\>  
\<% end %\>  
\<% end %\>  
\<% end %\>

I believe I can do something similar with @lpl\_short\_form\_app.practices  
🙂

I’ll try it out and report my results, for people who may be searching  
for similar results.

---

<div class="post-metadata">

**Author:** ![Ahad\_LSAmdani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7feea3/32.png) [@Ahad\_LSAmdani](https://www.ruby-forum.com/u/Ahad_LSAmdani)\
**Post date:** [May 6, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/n-m-relationship-joining-table-record-creation-issue/166947/4 "2009-05-06T23:13:15Z")

</div>

Ahad A. wrote:

> Thanks for the assistance. I’ve been looking at the guides, and I found  
> this in 7.3 of the forms guide:
> 
> \<% form\_for @person do |person\_form| %\>  
> \<%= person\_form.text\_field :name %\>  
> \<% for address in @person.addresses %\>  
> \<% person\_form.fields\_for address, :index =\> address do  
> |address\_form|%\>  
> \<%= address\_form.text\_field :city %\>  
> \<% end %\>  
> \<% end %\>  
> \<% end %\>
> 
> I believe I can do something similar with @lpl\_short\_form\_app.practices  
> 🙂
> 
> I’ll try it out and report my results, for people who may be searching  
> for similar results.

My resulting solution didn’t actually have much to do with this -  
however, it doesn’t take away any merits from this solution!

Good luck, and happy coding.
