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