Mi Ro
Mi Ro

Reputation: 760

Cannot destroy records for some models in rails

I have a model with 2 associations and no validations, which can be created but if I want to destroy it, I get error " ActiveRecord::RecordNotDestroyed" So I tried directly in rails create new record for this Model and then run destroy, but it rollbacks without any error message. If I do destroy! I get the error. I enabled mysql query logging but it looks it doesnt even get to delete part so I assume rails is somehow preventing from destroying this model.

As a test I created dummy model without any association, with just one string value. Same issue in rails console. I have to note that .delete method works, but .destroy not

Then I started to analyze in my rails app (I took it over from somebody so I am still not 100% familiar) and in rails console tried to destroy some records for different models and realized some I am able to destroy, some not.

Is there any way how to find out why I am not able to destroy those records? It is really strange for my Testmodel without any validation or association. Is there anywhere to look for more info? I tried this:

testrecord.destroy; testrecord.errors but in @messages I have empty {}

Here is my code for model where I found an issue (EinvoiceContact) and also my Testmodel

class EinvoiceContact < ActiveRecord::Base
  belongs_to :customer
  has_many :building
end

class Customer < ActiveRecord::Base

  has_many :einvoice_contacts, dependent: :destroy
  accepts_nested_attributes_for :einvoice_contacts, allow_destroy: true, reject_if: :empty_einvoice_contact

end

class Testmodel < ActiveRecord::Base
end

and here is output from rails and mysql log

> irb(main):045:0> Testmodel.find(1).destroy\r   Testmodel Load (0.4ms) 
> SELECT  `testmodels`.* FROM `testmodels` WHERE `testmodels`.`id` = 1
> LIMIT 1    (0.1ms)  BEGIN    (0.1ms)  ROLLBACK
> => false
> 
> 
> 2019-12-18T21:05:42.455378Z      33 Query SELECT  `testmodels`.* FROM
> `testmodels` WHERE `testmodels`.`id` = 1 LIMIT 1
> 2019-12-18T21:05:42.456559Z      33 Query BEGIN
> 2019-12-18T21:05:42.458261Z      33 Query ROLLBACK

Any help where to look for more details, or where this can be set is appreciated. This app uses CanCan but I have set

can :manage, :all

Rails is 4.2.8 with ruby 2.1.4 (I know it is old app, I just took it over and maintain), mysql thanks

Upvotes: 0

Views: 1280

Answers (3)

Mi Ro
Mi Ro

Reputation: 760

Problem solved, there was this code added in lib folder which prevents to destroy anything unless I set in model can_destroy to true

thanks all for help

class ActiveRecord::Base
  def can_destroy?
    false
  end

  before_destroy do
    can_destroy?
  end

Upvotes: 0

Fernand
Fernand

Reputation: 1333

It seems that Testmodel has some associations.
Try this in the console and check the output.

Testmodel.reflect_on_all_associations

Upvotes: 1

Kyle Heironimus
Kyle Heironimus

Reputation: 8041

Does Testmodel.find(1) return anything?

Try Testmodel.find(1).destroy! (with exclamation point) to see if it tells you anything more.

Upvotes: 0

Related Questions