# One to Many help

**URL:** <https://www.ruby-forum.com/t/one-to-many-help/75664>\
**Category:** Rails\
**Created:** [November 12, 2006, 2:03am UTC](https://www.ruby-forum.com/t/one-to-many-help/75664 "2006-11-12T02:03:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [November 12, 2006, 2:03am UTC](https://www.ruby-forum.com/t/one-to-many-help/75664/1 "2006-11-12T02:03:01Z")

</div>

I’m new to Ruby on Rails. I have two tables defined as  
Table 1 - Leagues  
id primary key - autonum  
txtLeagueName

Table 2 - Divisions  
id primary key - autonum  
league\_id  
txtDivisionName

I can add a League fine but when I try and add a division I get the  
error:  
“Couldn’t find League without an ID”

I’ve defined the League table with ‘has\_many :divisions’  
I’ve defined the Division table with ‘belongs\_to :league’

Here’s a snippet from my divison new.rhtml

**Division Name**

You can see I have included league\_id as an input field and have passed  
the value  
as a parameter in the url.(which is working fine, it shows up in the  
input field).  
When I click on the save button, that’s when I get the error above.  
I’m not sure  
what it’s trying to tell me let alone what to do to fix it.

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [November 12, 2006, 2:53am UTC](https://www.ruby-forum.com/t/one-to-many-help/75664/2 "2006-11-12T02:53:04Z")

</div>

Is your league\_id defined as a foreign key?

It seems like it’s complaining about not finding the league - are you  
trying to find the league in your new method of your controller? I  
would assume in the new method you would have something like

@league=League.find(params[:league\_id])

What file is the error referring to, the form or the controller?

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [November 12, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/one-to-many-help/75664/3 "2006-11-12T19:40:47Z")

</div>

I do have league\_id setup as a foreign key.

It’s complaining about my division controller. I do have what you  
mention regarding the league parm. Here is a copy of the controller.

class DivisionController \< ActionController::Base  
layout “division-layout”  
scaffold :division

before\_filter :initLeague

def index  
list  
render\_action ‘list’  
end

def new  
@division = @league.divisions.new  
end

def list  
@divisions = @league.divisions.find\_all  
end

def show  
@division = @league.divisions.find(params[:id])  
end

private  
def initLeague  
@league = League.find(params[:league\_id])  
@league\_name = @league.txtLeagueName  
end  
end

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [November 13, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/one-to-many-help/75664/4 "2006-11-13T16:11:22Z")

</div>

David F. wrote:

> > private  
> > def initLeague  
> > @league = League.find(params[:league\_id])  
> > @league\_name = @league.txtLeagueName  
> > end  
> > end
> 
> I think the problem lies in initLeague, where you are passing league\_id  
> as a parameter in the URL it is fine yes? However when you submit the  
> form, to get the league ID you will have to get  
> params[:division][:league\_id].
> 
> I don’t think its a good idea calling initLeague in before\_filter, you  
> would be better calling it from within the controller methods where it  
> is needed.

I’d add to that by saying that not all of those methods will actually  
have a league\_id passed as a param, so the before\_filter will fail  
often.

A.

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [November 13, 2006, 10:28am UTC](https://www.ruby-forum.com/t/one-to-many-help/75664/5 "2006-11-13T10:28:51Z")

</div>

> private  
> def initLeague  
> @league = League.find(params[:league\_id])  
> @league\_name = @league.txtLeagueName  
> end  
> end

I think the problem lies in initLeague, where you are passing league\_id  
as a parameter in the URL it is fine yes? However when you submit the  
form, to get the league ID you will have to get  
params[:division][:league\_id].

I don’t think its a good idea calling initLeague in before\_filter, you  
would be better calling it from within the controller methods where it  
is needed.
