# AssociationCollection or Array

**URL:** <https://www.ruby-forum.com/t/associationcollection-or-array/181243>\
**Category:** Rails\
**Created:** [January 8, 2010, 12:46am UTC](https://www.ruby-forum.com/t/associationcollection-or-array/181243 "2010-01-08T00:46:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cntrytwist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cntrytwist](https://www.ruby-forum.com/u/cntrytwist)\
**Post date:** [January 8, 2010, 12:46am UTC](https://www.ruby-forum.com/t/associationcollection-or-array/181243/1 "2010-01-08T00:46:57Z")

</div>

Could some kind soul help a confused programmer with a stupidly simple  
gotcha?

I have an association like this.

Project has\_and\_belongs\_to\_many Contracts  
Contracts has\_and\_belongs\_to\_many Projects

I want a simple array of contracts that belong to the project. Then I  
want to grab each contract and merge them into the array but I dont’  
want them associated to the project. Here is how I did it.

```

@record = Project.first
all_contracts = @record.contracts
@cts = Contracts.find(:all, :limit=>5)

@cts.each do |c|
  all_contracts << c
end

```

The issue is that the variable all\_contacts is an instance of  
AssociationCollection instead of Array. The subsequent “\<\<” calls  
actually create the associations (which I don’t want.)  
The only way I know of is to do the following

```

@record = Project.first
all_contracts = Array.new()
all_contracts = all_contracts | @record.contracts
@cts = Contracts.find(:all, :limit=>5)

@cts.each do |c|
  all_contracts << c
end

```

During the @cts.each do block, I am performing more than just adding  
to the all\_contracts array. How do I dry up the two lines that create  
the all\_contracts array? I want just an array, not an instance of  
AsociationCollection.

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![cntrytwist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cntrytwist](https://www.ruby-forum.com/u/cntrytwist)\
**Post date:** [January 8, 2010, 12:48am UTC](https://www.ruby-forum.com/t/associationcollection-or-array/181243/2 "2010-01-08T00:48:43Z")

</div>

On Jan 7, 11:26Â pm, cntrytwist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> During the @cts.each do block, I am performing more than just adding  
> to the all\_contracts array. Â How do I dry up the two lines that create  
> the all\_contracts array? Â I want just an array, not an instance of  
> AsociationCollection.

If you call to\_a on the association you’ll get a plain old array (that  
is the array that is the association’s backing store - if you’re  
planning on modifying it you should probably dup it)

Fred

---

<div class="post-metadata">

**Author:** ![cntrytwist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cntrytwist](https://www.ruby-forum.com/u/cntrytwist)\
**Post date:** [January 8, 2010, 2:09pm UTC](https://www.ruby-forum.com/t/associationcollection-or-array/181243/3 "2010-01-08T14:09:55Z")

</div>

Fred,  
Thanks for the reply. At the risk of looking like a total newby, what  
do you mean by dup it?  
I do appreciate the tip, and will use that to dry up my method a bit.

On Jan 7, 4:46Â pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cntrytwist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cntrytwist](https://www.ruby-forum.com/u/cntrytwist)\
**Post date:** [January 8, 2010, 2:17pm UTC](https://www.ruby-forum.com/t/associationcollection-or-array/181243/4 "2010-01-08T14:17:31Z")

</div>

On Jan 8, 1:08Â pm, cntrytwist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Fred,  
> Thanks for the reply. Â At the risk of looking like a total newby, what  
> do you mean by dup it?

Call dup on it.

Fred
