Posted almost 7 years ago. Visible to the public.

Unsaved record disappears when assigning to an association

If this happens to you:

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

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

Always do

Copy
class User < ActiveRecord::Base belongs_to :avatar end

and never

Copy
class User < ActiveRecord::Base belongs_to 'avatar' end

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Tobias Kraze
Last edit:
over 4 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Tobias Kraze to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more