Forum: Ruby on Rails order.products << product w/out persisting

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ben A. (Guest)
on 2006-03-09 00:47
(Received via mailing list)
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
Joe Van D. (Guest)
on 2006-03-09 00:47
(Received via mailing list)
Hm, maybe clone/dup the object?
Mark Reginald J. (Guest)
on 2006-03-09 01:15
(Received via mailing list)
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.
Ben A. (Guest)
on 2006-03-09 03:10
(Received via mailing list)
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
Mark Reginald J. (Guest)
on 2006-03-09 03:34
(Received via mailing list)
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> 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.
This topic is locked and can not be replied to.