How to Destroy a Record Without an Id Column in Ruby Activerecord

how can I destroy a record without an ID column in ruby ActiveRecord?

Have you tried using ActiveRecord's delete_all method? Here's an excerpt in a sandbox of mine:

>> customer = Customer.new(:login => 'foo')
=> #<Customer id: nil, status: 0, name: nil, login: "foo", password: nil, salt: nil, api_key: nil, address: nil, town: nil, state: nil, country: "USA", zipcode: nil, balance: #<BigDecimal:1032669b0,'0.0',9(18)>, discount: 0, last_four_cc: nil, auto_renew: false, contact_name: nil, contact_email: nil, domain: "anon.limelock.com", created_at: nil, updated_at: nil>
>> customer.save
=> true
>> Customer.all.count
=> 4
>> Customer.delete_all(:login => 'foo')
=> 1
>> Customer.all.count
=> 3

The generated SQL is DELETE FROM `customers` WHERE (`customers`.`login` = 'foo')

Check out the documentation

How to destroy/delete a Rails Active Record without an id column?

The reason User.where(name: 'Dummy').destroy doesn't work is User.where(name: 'Dummy') is a relation, not a single record. If you want, for example, to destroy all the records with name equal to 'Dummy', you can use destroy_all:

User.where(name: 'Dummy').destroy_all

How do you delete an ActiveRecord object?

It's destroy and destroy_all methods, like

user.destroy
User.find(15).destroy
User.destroy(15)
User.where(age: 20).destroy_all
User.destroy_all(age: 20)

Alternatively you can use delete and delete_all which won't enforce :before_destroy and :after_destroy callbacks or any dependent association options.

User.delete_all(condition: 'value') will allow you to delete records
without a primary key

Note: from @hammady's comment, user.destroy won't work if User model has no primary key.

Note 2: From @pavel-chuchuva's comment, destroy_all with conditions and delete_all with conditions has been deprecated in Rails 5.1 - see guides.rubyonrails.org/5_1_release_notes.html

Remove all records from a certain id onwards in rails?

first of all delete_all vs destroy_all in a short comparison:

  • delete_all: Will delete your data directly from the table without instantiating your ActiveRecord; Faster, but maybe not save depended on your model.
  • destroy_all: Your ActiveRecord will be instantiating and your Callbacks, Cascade Deletes and so on will be performed. Slower, but save.

If you have a RecordSet you always have to perfrom delete/destroy_all because the methods delete/destroy are only for a single ActiveRecord.

Sources:

  • https://apidock.com/rails/ActiveRecord/Relation/delete_all
  • https://apidock.com/rails/ActiveRecord/Relation/destroy_all

How to delete a record only if it exists in Rails?

Look at your Post model; post.rb

Ie.

Class Post < ActiveRecord::Base
has_many :download_tokens, dependent: :destroy
end

Now when you delete a Post object, whatever the association; has_many, has_one, it will find the destroy the dependent also. In this case the DownloadToken(s)

Couldn't find Object without an ID (Deleting a record)

In your index where you are creating the link, you aren't specifying the :id as an argument so your delete action has no params[:id]

Turn this:

<tr><%= link_to("Delete Action Item", {:action => 'delete'}) %></tr></br>

Into this:

<tr><%= link_to("Delete Action Item", {:action => 'delete', :id => display.id}) %></tr></br>

Remove the reference id from the record when the owner is deleted

use dependent: :nullify

Per the Rails guide:

:nullify causes the foreign key to be set to NULL. Callbacks are not
executed.

So you'd have:

class Category < ActiveRecord::Base
has_many: :posts,
dependent: :nullify
end

How would I destroy_all or delete_all records except one in ruby on rails?

This worked for me in the end.

Message.where('id != ? AND parent_id = ?', parent_id, parent_id).where(:sender_status => 1, :recipient_status => 1).delete_all

Basically returns all messages of that particular conversation except the one where id == parent_id. Whenever id == parent_id this means it's a parent message.

How can I delete some data from DB if there is no primary key?

You can use either of these two:

This will fire all destroy callbacks

Book.destroy_all( :book_id => 1 ) 

This won't

Book.delete_all( :book_id => 1 ) 


Related Topics



Leave a reply



Submit