Problem: Foreign Key in INSERT is NULL

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
fails.

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,

C

weird…

just a shot in the (very?) dark … did you define any association in
the model (models/Item.rb)?

My Item.rb is below:

class Item < ActiveRecord::Base

belongs_to :supplier, :foreign_key => “supplier_id”

end

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


@item.supplier_id=params[:record][:supplier_id]


@item.save

end

?

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,

C

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs