Questions about correct associations.

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( {|x| [,]} %> The use of 'category_id' and 'categories' I think is throwing me off. How does category relate to my wage model ?

TIA Stuart

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' %> <div id='select-b'> <%= select 'model','category_id', Model.find( {|x| [,]} %> </div>

#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', {|o| [,]} %>" end end

The thing that is throwing me here is the use of category_id and categories.


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


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 in Pay relates to the pay_id in the Wages table. Yet I keep coming up with nil errors.


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.


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 link 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' %> <div id='select-b'> <%= select 'model','category_id', Model.find( {|x| [,]} %> </div>

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


<%= select 'pay', 'id', Pay.find(:all).collect {|p| [,]}%> <div id='select-b'>

<%= select 'wage','pay_id', {|x| [,]} %>     </div>

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


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