I'm trying to delete, not destroy (that doesn't work either), a record as a learning exercise for Rails.
In the rails console, I execute:
NoMethodError: undefined method `name' for nil:NilClass
=> #<ActiveRecord::Associations::CollectionProxy [#<Page id: 2, subject_id: 1, name: "First Page", permalink: "first", position: 1, visible: false, created_at: "2016-08-19 17:58:02", updated_at: "2016-08-19 17:58:02">, #<Page id: 3, subject_id: 1, name: "Second Page", permalink: "second", position: 2, visible: false, created_at: "2016-08-19 17:59:07", updated_at: "2016-08-19 17:59:07">]>
According to the APIdock for
For has_many associations, the deletion is done according to the strategy specified by the :dependent option.[...] If no :dependent option is given, then it will follow the default strategy. The default strategy is :nullify. This sets the foreign keys to NULL.
That means the record is not destroyed, only the association.
You have to set the dependent option to
:destroy to delete the record from database.
class Subject < ActiveRecord::Base has_many :pages, dependent: :destroy end