# Active Record Insert relationship

**URL:** <https://www.ruby-forum.com/t/active-record-insert-relationship/140470>\
**Category:** Rails\
**Created:** [June 14, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/active-record-insert-relationship/140470 "2008-06-14T15:58:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodusa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rodusa](https://www.ruby-forum.com/u/rodusa)\
**Post date:** [June 14, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/active-record-insert-relationship/140470/1 "2008-06-14T15:58:53Z")

</div>

I need help inserting an address record to a tabled called realestates.  
The relationship between these to tables is 1 to 1.

- realestates table has an address\_id field set to not NULL

class Address \< ActiveRecord::Base  
end

class RealEstate \< ActiveRecord::Base  
has\_one :address  
end

I created two objects to match these two tables. When I let rails do the  
insert of RealEstate I get an error stating that address\_id is NULL.  
This means that I would need to insert the address first before  
inserting the RealEstate record to avoid this problem. But if Real  
Estate is new record it does not have an id yet and I can’t do  
that. One option is to remove the constraint NOT NULL I don’t want to do  
that.

So the logical order would be first create an instance variable for  
RealEstate and do the insert. Second, create and instance variable for  
address and do the insert like below.

@real\_estate = RealEstate.new(params[:real\_estate])  
@address = Address.new(params[:address])

But how do I solve the fact that address\_id cannot be NULL on  
realestates? Am I missing something?

Rod

---

<div class="post-metadata">

**Author:** ![rodusa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rodusa](https://www.ruby-forum.com/u/rodusa)\
**Post date:** [June 14, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/active-record-insert-relationship/140470/2 "2008-06-14T17:33:04Z")

</div>

On Jun 14, 2:58 pm, Rodrigo L. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> @real\_estate = RealEstate.new(params[:real\_estate])  
> @address = Address.new(params[:address])
> 
> But how do I solve the fact that address\_id cannot be NULL on  
> realestates? Am I missing something?

@address.build\_real\_estate(…) ?

Fred

---

<div class="post-metadata">

**Author:** ![rodusa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rodusa](https://www.ruby-forum.com/u/rodusa)\
**Post date:** [June 15, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/active-record-insert-relationship/140470/3 "2008-06-15T14:44:48Z")

</div>

On Sat, Jun 14, 2008 at 9:58 AM, Rodrigo L. \<  
removed\_email\_address@domain.invalid\> wrote:

> has\_one :address  
> end
> 
> I created two objects to match these two tables. When I let rails do the  
> insert of RealEstate I get an error stating that address\_id is NULL.

You appear to have a misconception about the relationship between  
has\_one/has\_many and the db schema.

First, you need to declare a belongs\_to relation to the ‘owning’ class  
in  
the owned class, so you need

class Address \< ActiveRecord::Base  
belongs\_to :real\_estate  
end

class RealEstate \< ActiveRecord::Base  
has\_one :address  
end

Second, the has\_x associations don’t require or use a foreign key field  
in  
the table of the class which has related objects, it’s the other way  
around. The belongs\_to association requires that the table of the class  
which belongs\_to another has a foreign key pointing to an instance of  
the  
owning class. So with the above declarations the addresses table should  
have a real\_estate\_id field not the other way around.

If you really want to have an address\_id in the real\_estates table then  
you  
should instead declare that RealEstate belongs\_to Address, and Address  
has\_one RealEstate.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rodusa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rodusa](https://www.ruby-forum.com/u/rodusa)\
**Post date:** [June 15, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/active-record-insert-relationship/140470/4 "2008-06-15T17:38:40Z")

</div>

thanks, I did change Address to include belongs\_to. But I still get the  
error:  
Mysql::Error: Column ‘address\_id’ cannot be null: INSERT INTO  
`real_estates`  
Should I create a address object, reference the realestate from it and  
save the address object which I assume would create a new real\_estate  
record? something like this:

@address = Address.new(params[:address])  
@address.realestate = RealEstate.new(params[:real\_estate])

instead of

@real\_estate = RealEstate.new(params[:real\_estate])  
@real\_estate.address = Address.new(params[:address])

this is the code below:

class Address \< ActiveRecord::Base  
belongs\_to :real\_estate  
end

class RealEstate \< ActiveRecord::Base  
has\_one :address  
end

class Admin::RealestateController \< ApplicationController

def index  
render :text =\> “Real Estate”  
end

def new  
render(:action =\> ‘new’)  
end

def create  
@real\_estate = RealEstate.new(params[:real\_estate])  
@real\_estate.address = Address.new(params[:address])

```
if @real_estate.save
  render :text => "Saved"
else
  render(:action => 'new')
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![rodusa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rodusa](https://www.ruby-forum.com/u/rodusa)\
**Post date:** [June 15, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/active-record-insert-relationship/140470/5 "2008-06-15T19:21:18Z")

</div>

On Sun, Jun 15, 2008 at 11:38 AM, Rodrigo L. \<  
removed\_email\_address@domain.invalid\> wrote:

> thanks, I did change Address to include belongs\_to. But I still get the  
> error:  
> Mysql::Error: Column ‘address\_id’ cannot be null: INSERT INTO  
> `real_estates`

Go back and re-read what I posted carefully.

> this is the code below:
> 
> class Address \< ActiveRecord::Base  
> belongs\_to :real\_estate

This declares that the addresses table should have a column called  
real\_estate\_id.

If you have an address object and have to fetch the corresponding  
real\_estate, then an SQL query

select \* from real\_estates where id = self.real\_estate\_id

end

class RealEstate \< ActiveRecord::Base  
has\_one :address

This does NOT imply that the real\_estates table has a column, called  
address\_id.

If you have an instance of RealEstate and need to get the corresponding  
address then the SQL query will be something like:

select \* from addresses limit 1 where real\_estate\_id = self.id

> end

If you really want the id of the Address in the RealEstate you need to  
set  
up the associations so that RealEstate belongs\_to Address, and Address  
has\_one RealEstate, which is the reverse of what you have declared.

You also need to make sure that the object with the has\_x attribute is  
saved  
before the one which belongs\_to it. This is because the belongs to  
object  
needs the id of the owner, and there won’t be one until the owner is  
saved.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rodusa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rodusa](https://www.ruby-forum.com/u/rodusa)\
**Post date:** [June 17, 2008, 12:05am UTC](https://www.ruby-forum.com/t/active-record-insert-relationship/140470/6 "2008-06-17T00:05:04Z")

</div>

Perfect. I am trying it out this weekend. Thank you.
