Difference between build_ and .build

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

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.

On Wed, Mar 28, 2012 at 8:41 PM, Tom T. [email protected] wrote:

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

ActiveRecord::Associations::ClassMethods

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