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.
5f699f59b58693ce313995caf4330486?d=identicon&s=25 Ben Anderson (Guest)
on 2006-03-08 23: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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-03-08 23:47
(Received via mailing list)
Hm, maybe clone/dup the object?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-09 00:15
(Received via mailing list)
Ben Anderson 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.
5f699f59b58693ce313995caf4330486?d=identicon&s=25 Ben Anderson (Guest)
on 2006-03-09 02: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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-09 02:34
(Received via mailing list)
Ben Anderson 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 James <mrj@bigpond.net.au> 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.