Rails optional belongs_to

Solution 1:

Just an update for rails 5, if you want this kind of behavior you will need to pass this option:

belongs_to :user, optional: true

In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default.

Update
If you still want to use the old behavior by default you can add the configuration to your application.rb file.

# /config/application.rb
config.active_record.belongs_to_required_by_default = false

notice: there were some issues on early releases of Rails 5 with this configuration, but is currently fixed, tested on the release candidate 5.2.3.

Solution 2:

Rails absolutely supports this out of the box, check your migrate, have you included a constraint such as :null => false on the user_id line? If so, take it out!

Edit: Or as @Rodrigo Dias states, reverse it to :null => true.

Also, check that you don't have any validations on the user relation in the Product model.