Active Record Insert relationship

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

@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

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.

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

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/

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