Unsaved record disappears when assigning to an association

If this happens to you:

user.avatar = Avatar.new
user.avatar  # => nil

(where avatar is a belongs_to), you probably declared your association incorrectly.

Always do

class User < ActiveRecord::Base
  belongs_to :avatar
end

and never

class User < ActiveRecord::Base
  belongs_to 'avatar'
end
Tobias Kraze Over 11 years ago