Being new to rails, I'm a little surprised that the rails migration/ActiveRecord does not create database level foreign keys for has_many
, has_one
and other relations. It is clear from searching on the topic, that this is the rails way.
I hit upon an example in the book Agile Web Development with Rails 4 which uses the following example on page 110.
class Product < ActiveRecord::Base
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
...
private
# ensure that there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
end
This example made me cringe because ensure_not_referenced_by_any_line_item
is exactly the kind of thing a programmer would forget to add. Also, in my opinion, it requires more lines of code meaning more bugs, etc.
I've found this thread which is more than five years old on the same topic. I've also become aware of the Foreigner Gem.
My question is regarding the current state of affairs in rails. Are database level foreign keys supported yet? Are there other options like the Foreigner Gem? I'm interested in Sqlite3 and MySQL backends.