Commit 4e642a56716091451c686815d64d57d4a30ba3eb
1 parent
cc382578
Exists in
colab
and in
4 other branches
Cascade user destroy to its attributes
Otherwise on databases that implement foreign keys it will raise a exception on destroying. See: https://lists.ime.usp.br/archives/mezuro-core/2015-August/005991.html
Showing
2 changed files
with
8 additions
and
8 deletions
Show diff stats
app/models/user.rb
| @@ -8,10 +8,10 @@ class User < ActiveRecord::Base | @@ -8,10 +8,10 @@ class User < ActiveRecord::Base | ||
| 8 | validates :email, presence: true | 8 | validates :email, presence: true |
| 9 | validates :email, uniqueness: true | 9 | validates :email, uniqueness: true |
| 10 | 10 | ||
| 11 | - has_many :project_attributes, class_name: 'ProjectAttributes' | ||
| 12 | - has_many :reading_group_attributes, class_name: 'ReadingGroupAttributes' | ||
| 13 | - has_many :repository_attributes, class_name: 'RepositoryAttributes' | ||
| 14 | - has_many :kalibro_configuration_attributes, class_name: 'KalibroConfigurationAttributes' | 11 | + has_many :project_attributes, class_name: 'ProjectAttributes', dependent: :destroy |
| 12 | + has_many :reading_group_attributes, class_name: 'ReadingGroupAttributes', dependent: :destroy | ||
| 13 | + has_many :repository_attributes, class_name: 'RepositoryAttributes', dependent: :destroy | ||
| 14 | + has_many :kalibro_configuration_attributes, class_name: 'KalibroConfigurationAttributes', dependent: :destroy | ||
| 15 | # Alert: when adding new parameters to this model, they should also be added to registrations_controller | 15 | # Alert: when adding new parameters to this model, they should also be added to registrations_controller |
| 16 | 16 | ||
| 17 | def projects | 17 | def projects |
spec/models/user_spec.rb
| @@ -10,10 +10,10 @@ describe User, :type => :model do | @@ -10,10 +10,10 @@ describe User, :type => :model do | ||
| 10 | end | 10 | end |
| 11 | 11 | ||
| 12 | describe 'associations' do | 12 | describe 'associations' do |
| 13 | - it { is_expected.to have_many(:project_attributes) } | ||
| 14 | - it { is_expected.to have_many(:reading_group_attributes) } | ||
| 15 | - it { is_expected.to have_many(:kalibro_configuration_attributes) } | ||
| 16 | - it { is_expected.to have_many(:repository_attributes) } | 13 | + it { is_expected.to have_many(:project_attributes).dependent(:destroy) } |
| 14 | + it { is_expected.to have_many(:reading_group_attributes).dependent(:destroy) } | ||
| 15 | + it { is_expected.to have_many(:kalibro_configuration_attributes).dependent(:destroy) } | ||
| 16 | + it { is_expected.to have_many(:repository_attributes).dependent(:destroy) } | ||
| 17 | end | 17 | end |
| 18 | 18 | ||
| 19 | describe 'methods' do | 19 | describe 'methods' do |