Forum: Ruby on Rails Problem: Foreign Key in INSERT is NULL

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ce9757fd7a542abb7065376f8dd6a9d2?d=identicon&s=25 Christopher Wood (rubyrubyruby_onrails)
on 2007-06-14 11:32
Using a collection_select in the view, I have added the foreign key to
the parameter list - good so far. The log file confirms this:

# The model is an item (in the Items database table), the supplier_id is
the foreign key in the same table.

Parameters: {"commit"=>"Create", "action"=>"create",
"controller"=>"item", "record"=>{"name"=>"My Object",
"description"=>"Large object", "supplier_id"=>"10001"}}

However, the next line in the log file says:

Cannot insert the value NULL into column 'supplier_id', table
'RubyOnRailsTestingDB.dbo.items'; column does not allow nulls. INSERT

Can anyone explain why the supplier_id would be null in the INSERT when
the parameters list clearly shows that the supplier_id is "10001"? (As
you can see, the other parameters are inserted properly.)

Thanks in advance,

4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 shai (Guest)
on 2007-06-14 11:38

just a shot in the (very?) dark ... did you define any association in
the model (models/Item.rb)?
Ce9757fd7a542abb7065376f8dd6a9d2?d=identicon&s=25 Christopher Wood (rubyrubyruby_onrails)
on 2007-06-14 11:40
My Item.rb is below:

class Item < ActiveRecord::Base

  belongs_to :supplier, :foreign_key => "supplier_id"

4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 shai (Guest)
on 2007-06-14 11:50
in the controller  Items#create:

did you assign the association? maybe becuase of the relashinship, rails
doesn't look at the column as a regular attribute, and you need to do
this manually, such as

def create

Ce9757fd7a542abb7065376f8dd6a9d2?d=identicon&s=25 Christopher Wood (rubyrubyruby_onrails)
on 2007-06-14 12:46
Shai, you're a genius. I've had that issue for days but no longer, and
if you'll pardon the pun, I'm finally back on track (rails - awful).

Many thanks,

This topic is locked and can not be replied to.