hi im new in Ruby on rails and currently working with a reservation system can anyone explain what is the difference between current_package = build_reservation_package(:package_id => package_id) abd current_package =reservation_package.build(:package_id => package_id) because i tried using the first one but no good but when i try the second it worked i think both are the same please correct me if im wrong thank -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
LED wrote in post #1053782:> hi im new in Ruby on rails and currently working with a reservation > system can anyone explain what is the difference between > current_package = build_reservation_package(:package_id => package_id) > abd > current_package =reservation_package.build(:package_id => package_id) > because i tried using the first one but no good but when i try the > second it worked i think both are the same please correct me if im > wrong thankI understand the difference between the two builds as follows: I am assuming you are using nested_attributes and if so then my understanding is that in your controller you will be building the reservation_package object so that your form has something to work with. If you have a has_many association then your controller will use the reservation_packages.build option and if you are working with a belongs_to association then you will find that the build_reservation_package is what is called for. I am new to rails as well, but that is what I have gleaned so far ... hope it helps. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Mar 28, 2012 at 8:41 PM, Tom Tom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> LED wrote in post #1053782: >> hi im new in Ruby on rails and currently working with a reservation >> system can anyone explain what is the difference between >> current_package = build_reservation_package(:package_id => package_id) >> abd >> current_package =reservation_package.build(:package_id => package_id) >> because i tried using the first one but no good but when i try the >> second it worked i think both are the same please correct me if im >> wrong thank > > I understand the difference between the two builds as follows: > I am assuming you are using nested_attributes and if so then my > understanding is that in your controller you will be building the > reservation_package object so that your form has something to work with. > If you have a has_many association then your controller will use the > reservation_packages.build option and if you are working with a > belongs_to association then you will find that the > build_reservation_package is what is called for. > > I am new to rails as well, but that is what I have gleaned so far ... > hope it helps.The table in http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html explains it quite well. For singular relations (has_one, belongs_to) "build_other" (SINGULAR word) is available For collection (has_many etc.) "others.build" (PLURAL word) is available. The really interesting part of these 2 build methods is that they delay database interactions until the "self" object is written to the database (and then the associated records are written together in one transaction, that fails or passes, all or nothing). HTH, Peter -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.