# Questions about correct associations

**URL:** <https://www.ruby-forum.com/t/questions-about-correct-associations/68016>\
**Category:** Rails\
**Created:** [September 8, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016 "2006-09-08T15:18:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 8, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016/1 "2006-09-08T15:18:11Z")

</div>

Currently have two models  
Pay and Wage

Pay has 2 columns (methods) id and name  
Wage has 3 columns id, pay\_id and name

Pay is set up with:  
has\_many :wages

Wage is:  
belongs\_to :pay

First, I believe this is set up correctly , according to Rails idiom.  
There are many wages that can be associate with a pay.

However in trying to map wage to pay I’m having a problem with this  
line:  
(This is an example line:)  
\<%= select ‘model’,‘category\_id’, Model.find(@model.id).categories.map  
{|x| [x.name, x.id]} %\>  
The use of ‘category\_id’ and ‘categories’ I think is throwing me off.  
How does category relate to my wage model ?

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 8, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016/2 "2006-09-08T17:54:30Z")

</div>

Maybe I should provide some additonal explanation in what I’m trying  
to accomplish. This is using the KRJS plugin.

In the example:

# View

\<%= select ‘model’,‘id’ %\>

\<%= select 'model','category\_id', Model.find(@model.id).categories.map {|x| [x.name, x.id]} %\>

#controller:  
def on\_model\_id\_change  
render :update do |page|  
@categories = Model.find(params[:dom\_value]).categories  
page.replace\_html ‘select-b’, :inline=\>"\<%= select  
‘model’,‘category\_id’, @categories.map {|o|  
[o.name, o.id]} %\>"  
end  
end

The thing that is throwing me here is the use of category\_id and  
categories.

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 8, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016/3 "2006-09-08T18:44:37Z")

</div>

Where does Model and it’s related categories field come from?

Jason

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 8, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016/4 "2006-09-08T19:40:45Z")

</div>

Model in the example is just a holder for the model name I believe.  
Categories in my model doesn’t exist.  
so maybe my associations are wrong.

In my thinking the pay.id in Pay relates to the pay\_id in the Wages  
table.  
Yet I keep coming up with nil errors.

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 8, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016/5 "2006-09-08T20:28:51Z")

</div>

Your associations and assumptions between Pay and Wage are correct from  
what  
you originally posted. Can you post the actual RHTML code that is  
suspect  
here? I’m having a hard time understanding the situation here.

Jason

On 9/8/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Model in the example is just a holder for the model name I believe.  
> Categories in my model doesn’t exist.  
> so maybe my associations are wrong.
> 
> In my thinking the pay.id in Pay relates to the pay\_id in the Wages  
> table.  
> Yet I keep coming up with nil errors.

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 8, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016/6 "2006-09-08T20:42:53Z")

</div>

\<%= select ‘pay’, ‘id’, Pay.find(:all).collect {|p| [p.name, p.id]}%\>

\<%= select ‘wage’,‘pay\_id’, @pay.wages.map {|x| [x.name, x.id]} %\>

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.wages

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 8, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016/7 "2006-09-08T20:50:54Z")

</div>

I’m just going to start this all over - cause I live on the ground  
floor so jumping isn’t going to help. I’ve also looked at it every  
which way and Sunday with no luck.  
I’m looking at this  
linkhttp://www.sciwerks.com/blog/2006/07/07/updating-select-controls-with-ajax/  
where it is talking about KRJS plugin. Regardless my particular  
question is not related to the plugin …yet.

The page is talking about updating a second select box based on the  
choice user makes in first. It goes on to show an example view:

# View

\<%= select ‘model’,‘id’ %\>

\<%= select 'model','category\_id', Model.find(@model.id).categories.map {|x| [x.name, x.id]} %\>

So in the example it’s clear that the 2nd select contains a column  
that is tied into the first select , category.

So taking my two models Pay and Wage - where Wage contains a method  
pay\_id I continue to get nil errors in the place @model.id)

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [September 9, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/questions-about-correct-associations/68016/8 "2006-09-09T16:56:29Z")

</div>

Well, do you ever set @pay in your controller?

Jason
