AssociationCollection or Array

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.

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

@cts.each do |c|   all_contracts << c end </code></pre>

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

<pre><code> @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 </code></pre>

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.

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

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.

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