# Question about using ".build" in association with has\_one and belongs\_to

**URL:** <https://www.ruby-forum.com/t/question-about-using-build-in-association-with-has-one-and-belongs-to/175256>\
**Category:** Rails\
**Created:** [September 10, 2009, 9:28am UTC](https://www.ruby-forum.com/t/question-about-using-build-in-association-with-has-one-and-belongs-to/175256 "2009-09-10T09:28:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [September 10, 2009, 9:28am UTC](https://www.ruby-forum.com/t/question-about-using-build-in-association-with-has-one-and-belongs-to/175256/1 "2009-09-10T09:28:32Z")

</div>

hi, guys,

I have the following db tables:

1. Addresses - represents an address (ie line 1, line 2,  
suburb,postcode, state and country)
2. Accounts - represents a user account. Will have a foreign key,  
“address\_id”
3. Warehouses - represents a warehouse. Will have a foreign key,  
“address\_id”

Each Account object can have one or zero (none) corresponding Address  
object.  
Each Warehouse object can have one or zero (none) corresponding  
Address object.

I started off reading

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

and

> **[Getting Started with Rails — Ruby on Rails Guides](https://guides.rubyonrails.org/getting_started.html#associating-models)**
>
> Getting Started with RailsThis guide covers getting up and running with Ruby on Rails.After reading this guide, you will know: How to install Rails, create a new Rails application, and connect your application to a database. The general layout of a...

on the “.build” helper.

From what I understand in context of the latter url, the model  
assigned with the “belongs\_to” will have a foreign key attribute.

In my case, consider that a new Account is being created (via  
registration).  
The user goes to his/her profile and clicks on the “create new  
Address” to fill in his/her address for the account.

The Addresses controller’s ‘new’ method is called.  
This is where I am stuck.

If i did something like

def new  
@address = @account.address.build  
end

this will cause a new Addresses object to be built with a non-existent  
attribute, :user\_id to be set with the new address’ id.

An option I have considered is to set the address id value into the  
Account object’s foreign key, ‘:address\_id’ when  
a new address is being submitted for creation (ie. calling Addresses  
controller method, ‘create’).  
Here’s an example:

```
def create
    @address = Address.new(params[:address])

    if @address.save
       @user.id = @address.id
       @user.save
       flash[:notice] = "Account registered!"
       redirect_back_or_default account_url
    else
      render :action => :new
    end
end

```

Is there a better way to set the foreign key of an object when an  
optionally associated object is being instantiated and saved to the  
db?

thanks
