Skip to content

Instantly share code, notes, and snippets.

@danielolivaresd
Created August 25, 2016 19:54
Show Gist options
  • Save danielolivaresd/146699b087a77784402f5eb7b768c59c to your computer and use it in GitHub Desktop.
Save danielolivaresd/146699b087a77784402f5eb7b768c59c to your computer and use it in GitHub Desktop.
class Device
belongs_to :car
end
class Car
has_one :device
end
# Rails adds foreign key constraints automatically, so sometimes (not sure when exactly),
# when you try to +destroy+ a Car, it will complain with a PG::ForeignKeyViolation: ERROR: ...
# We need to add a on_delete option from the database itself.
remove_foreign_key :devices, :cars
add_foreign_key :devices, :cars, on_delete: :nullify # Options are :nullify, :cascade and :restrict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment