rails/rails — Issue #4039: not set foreign key
Author: zinenko
Created: 2011-12-19 22:10:28+00:00
State: open

class User < ActiveRecord::Base
has_many :reports
end

class Report < ActiveRecord::Base
belongs_to :user
end

class SomeReport < Report
end

class Some2Report < Report
end

user = User.last
user.id => 3

report = Report.new

report.user = user
report.user_id => nil

report.save
# in log user_id is nil
report.reload
report.user => nil
report.user_id => nil

# ok ...

newreport = Report.new
newreport.user_id = user.id
newreport.user => nil
newreport.save
# user_id save to DB
newreport.reload
newreport.user_id == user.id # => true
newreport.user => nil

Gemfile # =>http://pastie.org/3042989
Gemfile.lock # => http://pastie.org/3042985