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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 7, 11:26 pm, cntrytwist <cntrytw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 7, 11:26 pm, cntrytwist <cntrytw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 8, 1:08 pm, cntrytwist <cntrytw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> I do appreciate the tip, and will use that to dry up my method a bit. > > On Jan 7, 4:46 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On Jan 7, 11:26 pm, cntrytwist <cntrytw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.