Why Can’t I Build Anything

Databases allow you to store information and store associations between different models. When first starting out in ruby you are able to create new instances of a model by using the .new and .create functions. The difference between the two is that .create automatically saves the instance to the table in your database.

As you progress you will want to be able to link your table together because just having buyers is boring. You want your buyer to be able to do other things something such as visit houses.

This is a nice way to like the models it is a lot of work. Also, you have to reload your buyer instance to be able to see the changes. Using the build action from ActiveRecord you can automatically create this connection.

Though the build method does not save automatically like the create method it does make updating and creating connections between models very easy.

The build method makes things easier, but you need to know when you can use it. In the example above this is a many-to-one relationship joined using the has_many and belongs_to methods. You can use this same system to join a many-to-many relationship.

Another relationship where you can use the build method is the one-to-one relationship joined using the has_one and belongs_to methods.

Though the syntax is a bit different because if you write agent.house.build it will through an error because agent.house evaluates to nil.

The build method is extremely useful and helps simplify coding. When trying to join tables in the future don't forget to use the build method.