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? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> 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? > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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_? On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> 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: > > 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?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) 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:> 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_? > > On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> 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: >> >>> 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? >>> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
wow, thanks william! On 10/1/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> > If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) > > 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: > 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_? > > On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > > 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: > > > 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? > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> wow, thanks william! > > On 10/1/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) >> >> 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: >> 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_? >> >> On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >> >> >> 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: >> >> >> 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? >> >> >> >> >> >> > >> >> > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On 10/1/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> > 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: > wow, thanks william! > > On 10/1/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > > If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) > > 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: > 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_? > > On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > > 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: > > > 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? > > > > > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> 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. > > On 10/1/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> 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: >> wow, thanks william! >> >> On 10/1/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >> >> >> If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) >> >> 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: >> 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_? >> >> On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >> >> >> 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: >> >> >> 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? >> >> >> >> >> >> > >> >> >> >> >> >> > >> >> > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
yeah that doesn''t seem to work for me. do you assume i don''t have my polymorphic association set up just right? On Oct 1, 11:54 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> 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: > > 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. > > > On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >> 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: > >> wow, thanks william! > > >> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >> If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) > > >> 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: > >> 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_? > > >> On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >> 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: > > >> 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?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> yeah that doesn''t seem to work for me. do you assume i don''t have my > polymorphic association set up just right? > > On Oct 1, 11:54 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> 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: >> >>> 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. >>> >>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>> >>>> 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: >>>> wow, thanks william! >>>> >>>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>>> >>>> If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) >>>> >>>> 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: >>>> 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_? >>>> >>>> On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>>> >>>> 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: >>>> >>>> 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? >>>> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You can see all of the methods that the different relationships add here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Scroll down and look at the section that documents belongs_to, etc and each lists the methods they add. mike wrote:> yeah that doesn''t seem to work for me. do you assume i don''t have my > polymorphic association set up just right? > > On Oct 1, 11:54 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> 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: >> >>> 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. >>> >>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>> >>>> 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: >>>> wow, thanks william! >>>> >>>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>>> >>>> If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) >>>> >>>> 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: >>>> 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_? >>>> >>>> On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>>> >>>> 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: >>>> >>>> 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? >>>> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Oct 2, 12:11 am, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> You can see all of the methods that the different relationships add here: > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMet... > > Scroll down and look at the section that documents belongs_to, etc and > each lists the methods they add. > > mike wrote: > > yeah that doesn''t seem to work for me. do you assume i don''t have my > > polymorphic association set up just right? > > > On Oct 1, 11:54 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >> 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: > > >>> 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. > > >>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>> 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: > >>>> wow, thanks william! > > >>>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>> If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) > > >>>> 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: > >>>> 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_? > > >>>> On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>> 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: > > >>>> 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?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> 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 > > > On Oct 2, 12:11 am, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> You can see all of the methods that the different relationships add here: >> http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMet... >> >> Scroll down and look at the section that documents belongs_to, etc and >> each lists the methods they add. >> >> mike wrote: >> >>> yeah that doesn''t seem to work for me. do you assume i don''t have my >>> polymorphic association set up just right? >>> >>> On Oct 1, 11:54 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>> >>>> 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: >>>> >>>>> 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. >>>>> >>>>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>>>> >>>>>> 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: >>>>>> wow, thanks william! >>>>>> >>>>>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>>>>> >>>>>> If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) >>>>>> >>>>>> 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: >>>>>> 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_? >>>>>> >>>>>> On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: >>>>>> >>>>>> 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: >>>>>> >>>>>> 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? >>>>>> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Oct 2, 4:09 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> 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: > > 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 > > > On Oct 2, 12:11 am, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >> You can see all of the methods that the different relationships add here: > >> http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMet... > > >> Scroll down and look at the section that documents belongs_to, etc and > >> each lists the methods they add. > > >> mike wrote: > > >>> yeah that doesn''t seem to work for me. do you assume i don''t have my > >>> polymorphic association set up just right? > > >>> On Oct 1, 11:54 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>> 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: > > >>>>> 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. > > >>>>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>>>> 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: > >>>>>> wow, thanks william! > > >>>>>> On 10/1/07, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>>>> If it''s polymorphic, just treat it if it''s not, thats the beauty of it. :) > > >>>>>> 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: > >>>>>> 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_? > > >>>>>> On Oct 1, 9:20 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > >>>>>> 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: > > >>>>>> 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?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---