# Collection.pop

**URL:** <https://www.ruby-forum.com/t/collection-pop/65748>\
**Category:** Rails\
**Created:** [August 21, 2006, 2:43am UTC](https://www.ruby-forum.com/t/collection-pop/65748 "2006-08-21T02:43:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [August 21, 2006, 2:43am UTC](https://www.ruby-forum.com/t/collection-pop/65748/1 "2006-08-21T02:43:51Z")

</div>

Hi everyone,

I have a category which has\_many :items, and I’m trying to use

category.items.pop

Only, the last element in items isn’t being removed. Is there a way to  
change  
this easily?

Thanks  
Daniel

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [August 21, 2006, 3:11am UTC](https://www.ruby-forum.com/t/collection-pop/65748/2 "2006-08-21T03:11:53Z")

</div>

Right now I’m just using category.items.to\_a and that works fine

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [August 21, 2006, 3:21am UTC](https://www.ruby-forum.com/t/collection-pop/65748/3 "2006-08-21T03:21:53Z")

</div>

Hi –

On Sun, 20 Aug 2006, Daniel H. wrote:

> Hi everyone,
> 
> I have a category which has\_many :items, and I’m trying to use
> 
> category.items.pop
> 
> Only, the last element in items isn’t being removed. Is there a way to change  
> this easily?

If you want a pop that matches the push, you can do:

class ActiveRecord::Associations::AssociationCollection  
def pop  
load\_target  
delete(@target[-1])  
end  
end

Tested only very informally, and comes with the usual caveats about  
changing default behavior.

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [August 21, 2006, 3:49am UTC](https://www.ruby-forum.com/t/collection-pop/65748/4 "2006-08-21T03:49:54Z")

</div>

What I’m using in full is actually

def items\_array  
@items\_array ||= items.to\_a  
end

so I then have category.items\_array.pop and the element is deleted

But I like your suggestion better, thanks!

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [August 21, 2006, 3:23am UTC](https://www.ruby-forum.com/t/collection-pop/65748/5 "2006-08-21T03:23:53Z")

</div>

Hi –

On Sun, 20 Aug 2006, Daniel H. wrote:

> Right now I’m just using category.items.to\_a and that works fine

OK… then don’t do what I suggested in my last post 🙂 I thought  
you wanted pop to actually delete the association.

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [August 21, 2006, 3:57am UTC](https://www.ruby-forum.com/t/collection-pop/65748/6 "2006-08-21T03:57:55Z")

</div>

Hi –

On Sun, 20 Aug 2006, Daniel H. wrote:

> What I’m using in full is actually
> 
> def items\_array  
> @items\_array ||= items.to\_a  
> end
> 
> so I then have category.items\_array.pop and the element is deleted
> 
> But I like your suggestion better, thanks!

As long as it’s clear that with my technique, you’re actually changing  
the database when you use ‘pop’. (I imagine it is clear but I just  
want to rest with an easy conscience tonight 🙂

David

> > David
> 
> >

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.
