temporary active records

I would like to create some activerecord instances in my application that don't ever get written to the database while others do. Is that possible?

Sure, use either "new" or through associations "build_foo" foo being the association.

@f = Foo.new(hash_of_foo_stuff) #this is not saved @f.save #now it is @b = @f.build_bar(hash_of_bar_stuff) #not saved @b.save #now it is

mike wrote:

hmm, cool, what if the association is polymorphic? what would foo be in build_foo? or is there some other way to specify what class i want an instance of with build_?

If it’s polymorphic, just treat it if it’s not, thats the beauty of it. :slight_smile:

In the console, create an object and take a look at it’s build methods like this:

puts Foo.find(:first).methods.grep(/build_/).sort.join(“\n”)

mike wrote:

wow, thanks william!

One thing to note. Be careful of creating associations off of objects that will get saved when you don’t want the associated object saved. When an association gets saved in relation to it’s parent object is not always apparent. However, if you create an association off of an object you never intend to save, this is not a worry. For more on when things get saved, look in the Agile Web Development with Rails book. In the later ActiveRecord chapter, there is a section on “when things get saved”.

Michael Bannister wrote:

I got excited by your previous post and thought I understood but I still haven't figured out how to make it work the way I'd like. The only build_ methods available on the object i want to associate temporary (polymorphic) subobjects to is build_to_linkable (linkable being the name I chose for my polymorphic association). So I'm still not sure how to let the parent object know what the actual type is that I'm linking to it. I'm assuming build_to_linkable takes a hash which would be the same hash that LinkableClass.create would take? I am also saving the parent object but some child objects are supposed to be temporary. Thanks for all your help.

The polymorphic methods probably won’t show up since there is not a explicit relationship. Have you tried to call them? Say a linkable could be an Image, try:

f = Foo.find(:first)

f.build_image(image_info_hash)

That should work, but let me know if it doesn’t and I’ll dig around for some examples in my code.

Michael Bannister wrote:

yeah that doesn't seem to work for me. do you assume i don't have my polymorphic association set up just right?

Is it a has_many or has_one relationship? build_ method are only available on relationships of a single object. Multiple object relationships, like has_many, etc use different methods. If it is a has_many for example, you can do:

f = Foo.new(foo_parms)

f.bars.new(bars_parms)

This would create it and associate the two, but not save them.

mike wrote:

You can see all of the methods that the different relationships add here:

Scroll down and look at the section that documents belongs_to, etc and each lists the methods they add. mike wrote:

Cool, I think I'm getting somewhere. Thanks for all your help William. I got something like this to work:

@main_menu.linkings.build(:linkable => Url.new(:path => "/ account", :name => "My Account"))

This should be ok but I still wonder why I can't use the syntax you described... any further suggestions?

Regards, Mike

This is the syntax I described. Since linkings are apparently a has_many relationship, the are built the way you show. If they were a has_one relationship, it would be build_linking. Seems like you are on track!

mike wrote:

Ok great. Thanks again William for helping me figure all that out...

Previously I had an 'add' instance method on my menu class but since I have some objects that I want to be persistent and others that aren't I'm trying to decide on an approach. Right now I'm favoring passing in a parameter to 'add' telling it if it's persistent or not. Or I might give child class or instances of the child class an attribute that determines if it's saved. I welcome any suggestions especially if there's a ruby or oo-related best practice.

Mike