Earlier when I tried to destroy a record from a model I was getting a Foreign key constraint error, so I added the dependent: :destroy option in the relationships. This is how the relationships look like now:
class Package < ActiveRecord::Base
has_one :package_event, dependent: :destroy
has_one :tag_package, dependent: :destroy
has_one :current_bin, :through => :tag_package
has_many :tag_packages, lambda{ with_deleted }, dependent: :destroy
has_one :tag, :through => :tag_package
has_one :tag_type, :through => :tag
has_one :tag_issuer, :through => :tag
has_many :scans, lambda{ distinct }, :through => :tag
But now when I am trying to destroy a record from the model I am getting an ActiveRecord::RecordNotDestroyed and it is not showing the error message so I am not sure what is the reason behind this error.
>> Package.first.destroy!
=> ActiveRecord::RecordNotDestroyed (Failed to destroy the record)
I am suspecting this has something to do with the has_many_through relation that is going on. Does anyone know how to fix this ?
You can rescue the error (rescue ActiveRecord::RecordNotDestroyed => error) and look at error.record.errors.
I found the solution from this answer: https://stackoverflow.com/a/53872915/4785305
It could be you have a before_destroy callback preventing one of the associated records from being destroyed.
Related
I have the following Model
class Will < ApplicationRecord
belongs_to :user
belongs_to :product
has_one :willFamilyDetail, :dependent => :destroy
has_one :childCompensate, :dependent => :destroy
has_one :wifeCompensate, :dependent => :destroy
has_one :willDebt, :dependent => :destroy
has_one :willLegalHeirBequest, :dependent => :destroy
has_one :willGrandchildrenBequest, :dependent => :destroy
has_one :willBequestOther, :dependent => :destroy
end
and all other models have belongs_to.
When I use Rails console to delete the Will object, other objects still appear in the database, they must get destroyed right?
What am I doing wrong?
ActiveRecord association symbols must be snake case, not camel case, following Ruby convention. Converting each association name from camel case to snake case (:willFamilyDetail to :will_family_detail, etc.) solves the issue.
I have these models
class User < ActiveRecord::Base
has_many :user_functions, :dependent => :destroy
has_many :functions, :through => :user_functions
accepts_nested_attributes_for :functions, allow_destroy: true
Model of the linked table:
class UserFunction < ActiveRecord::Base
belongs_to :user, inverse_of: :user_functions
belongs_to :function, inverse_of: :user_functions
after_destroy :unplan_items
after_create :plan_items
and of course the model of function but this is like user...
Now when I do the following in my tests:
#user.functions = [#functions]
#user.save
expect(#user.planned_items.count).to eq(1)
#user.functions = []
#user.save
I notice the callback after_destroy isn't called. Why is this and how can I avoid this. There are certain steps that need to be done every time a UserFunction is destroyed...
I believe this has to do with: https://github.com/rails/rails/issues/7618 (I'm using rails 4.2.5 though). The after_create is working perfect though...
Currently rails uses :delete_all as default strategy of has_many_through. It only calls :destroy_all when we explicitly specify dependent: :destroy on the association.
The docs mention advice to use has_many :through if you need callbacks:
See the suggestion here: http://guides.rubyonrails.org/association_basics.html
You should use has_many :through if you need validations, callbacks,
or extra attributes on the join model.
So there currently is an inconsistency between after_create which does do the callback and after_destroy.
This is mentioned in these two issues posted on GitHub:
https://github.com/rails/rails/issues/7618
https://github.com/rails/rails/issues/27099
The fix for now is to explicitly put :dependent => :destroy on the :through part. This will make sure the callback are used.
class User < ActiveRecord::Base
has_many :user_functions
has_many :functions, :through => :user_functions, :dependent => :destroy
accepts_nested_attributes_for :functions, allow_destroy: true
For anyone reading this 2021+
Change This
has_many :object_tags, :as => :taggable, :dependent => :destroy
has_many :tags, :through => :object_tags
To This
has_many :object_tags
has_many :tags, :through => :object_tags, :dependent => :destroy
I have a User model with a has_many :through relationship to the Publication model. The Publication model in turn has a has_many :through relationship to Author:
class User < ActiveRecord::Base
has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
has_many :publications, :through => :library_publications
end
class Library::Publication < ActiveRecord::Base
belongs_to :publication
belongs_to :user
end
class Publication < PublicationBase
has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
has_many :users, :through => :library_publications
has_many :publication_contributions, :dependent => :destroy, :class_name => "Publication::Contribution"
has_many :authors, :through => :publication_contributions
end
class Author < AuthorBase
has_many :publication_contributions, :dependent => :destroy, :class_name => "Publication::Contribution"
has_many :publications, :through => :publication_contributions
end
class Publication::Contribution < Publication::ContributionBase
belongs_to :publication, :class_name => "Publication"
belongs_to :author, :class_name => "Author"
end
As far as I can tell, all the associations are written correctly. However, when I try to eagerload authors from a user:
#user.library_publications.includes(:publication => [:authors])
I get this error:
Association named 'authors' was not found; perhaps you misspelled it?
What might be the cause of this?
After experimenting a little, I discovered that all of publication's associations were broken. This led to me to looking for larger problems, and eventually I discovered that this issue was caused by one of the join-table being namespaced, Library::Publication. When I de-namespaced it, publication's associations began working again.
I'm not sure why this happened, though. If anyone has an explanation, please share.
I am using Ruby on Rails 3.1.0 and I would like to know why database records in the following example are not deleted on destroying an "associated" model instance.
In my application I have these classes:
class User < ActiveRecord::Base
has_one :account,
:autosave => true,
:dependent => :destroy
has_many :article_relationships,
:class_name => 'ArticleUserRelationship',
:foreign_key => 'user_id',
:autosave => true,
:dependent => :destroy
has_many :articles,
:through => :article_relationships,
:source => :article,
:dependent => :destroy
...
end
class ArticleRelationship < ActiveRecord::Base
belongs_to :user
...
end
class Account < ActiveRecord::Base
belongs_to :user,
:autosave => true,
:dependent => :destroy
end
If I run #article.user.destroy or #article.user.account.destroy methods (note: article is kind of Article) those will delete from the database the user, the account and article relationship records but will not delete users related articles, even if I state has_many :articles ... :dependent => :destroy.
Anyway I read the official RoR guide about the "has_many Association Reference" and at the 4.3.2.6 :dependent chapter it says:
This option (:dependent) is ignored when you use the :through option on the association.
So, what could/should I make in order to delete all user articles? That is, what method could/should I run? How?
I am having an issue when trying to destroy an active record instance.
It involves the following AR
class Client < ActiveRecord::Base
has_many :phone_numbers, :dependent => :destroy
has_many :email_addresses, :dependent => :destroy
has_many :user_clients , :dependent => :destroy
has_many :users, :through => :user_clients
end
class UserClient < ActiveRecord::Base
belongs_to :user
belongs_to :client , :dependent => :destroy
has_many :instructions, :dependent => :destroy
end
When performing a destroy on a Client instance I am given the following error
#dead_man = Client.find(params[:id])
#dead_man.destroy => uninitialized constant UserClient::Instruction
I am really not sure where this error is coming from. Any help is greatly appreciated!
It's not finding your Instruction model. Make sure it's in the models directory, appropriately named, extends ActiveRecord::Base, etc.
Also, you should remove the :dependent => :destroy from the belongs_to :client line in the UserClient model, unless you really want deletion of a user_client to result in deletion of the client. It sounds like it should be the other way around, and that's already set up in the Client model.
Also check that the file name corresponds with the class name. In my case I had
Class NameSpace::MyStats
in
namespace/old_stats.rb
and Rails kept on throwing the "uninitialized constant error" until I changed it to
namespace/my_stats.rb