# Order.products \<\< product w/out persisting

**URL:** <https://www.ruby-forum.com/t/order-products-product-w-out-persisting/78761>\
**Category:** Rails\
**Created:** [March 8, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/order-products-product-w-out-persisting/78761 "2006-03-08T23:47:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 8, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/order-products-product-w-out-persisting/78761/1 "2006-03-08T23:47:40Z")

</div>

Hi,  
I have a one-to-many relationship. I want to be able to add objects  
to the collection w/out persisting them. How do I do this?

The only thing I can’t think of is to use a transaction and roll it  
back.

In other words - I want to do this:

o = Order.find(1)  
p = Product.find(1)  
o.products \<\< p # but don’t persist this

I assume I’m just missing something obvious here. Any ideas?  
Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 8, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/order-products-product-w-out-persisting/78761/2 "2006-03-08T23:47:41Z")

</div>

Hm, maybe clone/dup the object?

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 9, 2006, 12:15am UTC](https://www.ruby-forum.com/t/order-products-product-w-out-persisting/78761/3 "2006-03-09T00:15:02Z")

</div>

Ben A. wrote:

> I have a one-to-many relationship. I want to be able to add objects  
> to the collection w/out persisting them. How do I do this?
> 
> The only thing I can’t think of is to use a transaction and roll it back.
> 
> In other words - I want to do this:
> 
> o = Order.find(1)  
> p = Product.find(1)  
> o.products \<\< p # but don’t persist this

o.products.to\_ary \<\< p

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 9, 2006, 2:10am UTC](https://www.ruby-forum.com/t/order-products-product-w-out-persisting/78761/4 "2006-03-09T02:10:15Z")

</div>

yes, but I still want it to be there as an unpersisted member, so I  
get it when I do

o.products.each do |p|  
…  
end

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [March 9, 2006, 2:34am UTC](https://www.ruby-forum.com/t/order-products-product-w-out-persisting/78761/5 "2006-03-09T02:34:29Z")

</div>

Ben A. wrote:

> yes, but I still want it to be there as an unpersisted member, so I  
> get it when I do
> 
> o.products.each do |p|  
> …  
> end
> 
> On 3/8/06, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > o.products.to\_ary \<\< p

Hmm, I’m surprised this doesn’t work.

–  
We develop, watch us RoR, in numbers too big to ignore.
