9

How can i remove the last element from an ActiveRecord_Relation in rails?

e.g. if I set:

@drivers = Driver.all

I can add a another Driver object called @new_driver to @drivers by doing:

@drivers << @new_driver

But how can I remove an object from @drivers?

The delete method doesn't seem to work, i.e.

@drivers.delete(0)
3
  • 1
    what is the association name ? Commented Jun 24, 2014 at 8:59
  • 1
    @drivers.delete(@new_driver) ? Commented Jun 24, 2014 at 9:00
  • Sorry, to clarify. I'm not looking to delete the object from the DB, only from the collection. I'm using the collection as an option list within a form and am continually changing/updating it. Given this, I suppose neither the delete nor destroy methods are appropriate? Commented Jun 24, 2014 at 9:34

6 Answers 6

12

You can use the reject! method, this will remove the object from the collection without affecting the db

for example:

driver_to_delete = @driver.first # you need the object that you want removed
@drivers.reject!{|driver| driver == driver_to_delete}
2
  • 2
    Unfortunately this no longer seems to work in Rails 6.1 – I'm not sure when this changed, but here's the error message: NoMethodError: undefined method reject!' for #<ActiveRecord::Relation […]`
    – Marc
    Commented Jun 2, 2021 at 23:57
  • @Marc and @cousine please check my answer to understand why reject! cannot work.
    – gfd
    Commented Mar 23, 2022 at 22:13
6

Very late too, but I arrived here looking for a fast answer and finished by thinking by myself ;)

Just to clarify about the different answers and the Rails 6.1 comment on accepted answer:

The OP wanted to remove one entry from a query, but NOT remove it from database, so any answer with delete or destroy is just wrong (this WILL delete data from your database !!).

In Ruby (and therefore Rails) convention, shebang methods (ending with !) tend to alter the given parameter. So reject! would imply modifying the source list ... but an ActiveRecord_Relation is basically just a query, NOT an array of entries !


So you'd have 2 options:

  • Write your query differently to specifically say you don't want some id:
@drivers.where.not(id: @driver_to_remove)  # This still is an ActiveRecord_Relation
  • Use reject (NO shebang) on your query to transform it into an Array and "manually" remove the entry you don't want:
@drivers.reject{ |driver| driver ==  @driver_to_remove}
# The `reject` forces the execution of the query in DB and returns an Array)

On a performance point of view, I would personally recommend the first solution as it would be just a little more complex against the DB where the latter implies looping on the whole (eventually large) array.

4

Late to the question, but just had the same issue and hope this helps someone else.

reject!did not work for ActiveRecord_Relation in Rails 4.2

drop(1) was the solution

In this case @drivers.drop(0) would work to drop the first element of the relation

1
  • 6
    but one thing i noticed is that class of collection will become an Array and other Active Record Relation methods cannot be used.
    – pk-n
    Commented Apr 21, 2017 at 14:03
1

Since its an array of objects, have you tried to write something like @drivers.delete(@new_driver) or @drivers.delete(id: @new_driver.id) ?

1
  • 5
    Warning: This deletes the record from the database! It's quite unexpected because on a normal array that would indeed do what you'd expect. But on an ActiveRecord collection it will execute a DELETE statement.
    – Marc
    Commented Jun 2, 2021 at 23:54
1

This is the documentation you need:

@group.avatars << Avatar.new
@group.avatars.delete(@group.avatars.last)

--

.destroy

The problem you've got is you're trying to use collection methods on a non-collection object. You'll need to use the .destroy ActiveRecord method to get rid of the record from the database (and consequently the collection):

@drivers = Driver.all
@drivers.last.destroy

--

Scope

.delete will remove the record from the DB

If you want to pull specific elements from the db to populate the @drivers object, you'll need to use a scope:

#app/models/driver.rb
Class Driver < ActiveRecord::Base
   scope :your_scope, -> { where column: "value" }
end

This will allow you to call:

#app/controllers/drivers_controller.rb
def index
   @drivers = Driver.your_scope
end

I think you're getting the MVC programming pattern confused - data manipulation is meant to happen in the model, not the controller

1
  • 6
    Sorry, to clarify. I'm not looking to delete the object from the DB, only from the collection. I'm using the collection as an option list within a form and am continually changing/updating it. Given this, I suppose neither the delete nor destroy methods are appropriate? Commented Jun 24, 2014 at 9:33
0

As stated above, reject! doesn't work in Rails 4.2, but delete does, so @drivers.delete(@new_driver) works, and more generally:

@drivers.delete(Driver.where(your condition))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.