Topic: Join table is not being updated as expected

Hi,

I have the following problem, one of my join tables (Armycompo) is not being updated at all!

The models are:

class Unit < ActiveRecord::Base 
  belongs_to :game
  has_many :sceunits
  has_many :armies, :through => :armycompo
 
class Army < ActiveRecord::Base
  belongs_to :game
  has_many :scenarioarmies
  has_many :units, :through => :armycompo

class Armycompo < ActiveRecord::Base
 
  belongs_to :army
  belongs_to :unit
end

When I capture the form contents (via the development log) it shows that the form was populated correctly, but the INSERT into command is populating with nulls - see extract from development log below:

Processing ArmycompoController#create (for 127.0.0.1 at 2007-01-22 15:52:40) [POST]
  Session ID: 6881dac28ab3c1195581b76c52831389
  Parameters: {"commit"=>"Create", "action"=>"create", "controller"=>"armycompo", "armycompo"=>{"unit_id"=>"2", "army_id"=>"13"}}
  Armycompo Columns (0.016000)   SHOW FIELDS FROM armycompos
  SQL (0.000000)   BEGIN
  SQL (0.000000)   INSERT INTO armycompos (`unit_id`, `army_id`) VALUES(NULL, NULL)
  SQL (0.047000)   COMMIT
Redirected to http://localhost:3000/armycompo/list
Completed in 0.07800 (12 reqs/sec) | DB: 0.06300 (80%) | 302 Found [http://localhost/armycompo/create]

I am using the same principle/models for other parts of my application and they are working as expected, ie the db join table is being updated accordingly.

Any help is appreciated,

Thanks in advance,

Steve.

Re: Join table is not being updated as expected

Can you post the code in the "create" action in the Armycompo controller?

Railscasts - Free Ruby on Rails Screencasts

Re: Join table is not being updated as expected

Code as requested.

def create
    @compo = Armycompo.new(@params['compo'])
    if  @compo.save
        redirect_to :action => 'list'
    else
        render_action 'new'
    end
  end

Re: Join table is not being updated as expected

Try this instead:

  def create
    @compo = Armycompo.new(params['armycompo'])
    if  @compo.save
        redirect_to :action => 'list'
    else
        render_action 'new'
    end
  end

If you look at the parameters passed in the logs, the values are in the "armycompo" hash, not "compo".

Railscasts - Free Ruby on Rails Screencasts

Re: Join table is not being updated as expected

Doh! Of course - sorry.

Re: Join table is not being updated as expected

I've applied that fix, but I'm still getting the same results - the INSERT statement is trying to populate my join table with nulls. A row is created in the join table - but with NULLS where I'm expecting the army_id & unit_id values.

Any ideas?

Re: Join table is not being updated as expected

Problem resolved - I capitalised the fix you supplied.