Read more

Unsaved record disappears when assigning to an association

Tobias Kraze
September 25, 2012Software engineer at makandra GmbH

If this happens to you:

user.avatar = Avatar.new
user.avatar  # => nil
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

(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
Posted by Tobias Kraze to makandra dev (2012-09-25 14:35)