Calin
Calin

Reputation: 6867

Rails weird delete behaviour

Sorry for the "weird" in the subject, here is what is happening.

I have a ActiveRecord object that looks like:

class BraintreeCache < ActiveRecord::Base
belongs_to :company, :foreign_key => :subscription_id
serialize :subscription
serialize :credit_card
end

Executing:

BraintreeCache.delete(:subscription_id => "12")

Will return:

ActiveRecord::StatementInvalid: PGError: ERROR:  missing FROM-clause entry for table "id"
LINE 1: DELETE FROM "braintree_caches" WHERE "id"."subscription_id" ...
                                             ^
: DELETE FROM "braintree_caches" WHERE "id"."subscription_id" = '12'

What is going on here? Where does the "id" come from?

Thank you,

Upvotes: 0

Views: 97

Answers (3)

Thilo
Thilo

Reputation: 17735

The static delete method expects an ID as the first argument, not a hash. Try this:

BraintreeCache.find_by_subscription_id("12").delete

EDIT:

If you don't want to have to instantiate the object first, you could also use the delete_all method:

BraintreeCache.delete_all(subscription_id: "12")

Upvotes: 0

denis.peplin
denis.peplin

Reputation: 9851

It probably comes from misusing of delete method. Try this instead:

BraintreeCache.where(:subscription_id => "12").delete_all

Upvotes: 0

Calin
Calin

Reputation: 6867

So what got me very confused is that I have branch this delete is working on.

The correct syntax should be:

BraintreeCache.delete_all(:subscription_id => "42")

I still can't figure out how the simple delete was working.

Regards,

Upvotes: 1

Related Questions