Foreign keys not being saved by nested forms

View: New views
4 Messages — Rating Filter:   Alert me  

Foreign keys not being saved by nested forms

by Todd A. Jacobs-20 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I have the following included in my schema:

  create_table "users", :force => true do |t|
    t.string   "login"
    t.string   "first_name"
    t.string   "last_name"
    t.string   "email"
    t.string   "password"
    t.integer  "contact_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  create_table "contacts", :force => true do |t|
    t.string   "company_name"
    t.integer  "address_id"
    t.integer  "phone"
    t.string   "website"
    t.integer  "biztype_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  create_table "addresses", :force => true do |t|
    t.string   "street_address1"
    t.string   "street_address2"
    t.string   "city"
    t.integer  "state_id"
    t.integer  "zipcode"
    t.integer  "plus4"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

In other words, a user has a foreign key linking to the contacts table,
and a contact is linked to an address. In my models, I have defined:

    class User < ActiveRecord::Base
        has_one :contact
        has_one :address, :through => :contact
        has_many :properties
        has_many :notes, :through => :properties

        accepts_nested_attributes_for :contact, {
            :allow_destroy => true,
            :reject_if => :all_blank
        }
        accepts_nested_attributes_for :address, {
            :allow_destroy => true,
            :reject_if => :all_blank
        }
    end
    class Contact < ActiveRecord::Base
        belongs_to :user
        has_one :address
        has_one :biztype
    end
    class Address < ActiveRecord::Base
        belongs_to :contact
        belongs_to :property
    end

In order to create a nested form, my AccountsController says:

  def new
    @user = User.new
    @user.build_contact
    @user.contact.build_address
    respond_to do |format|
      format.html
      format.xml  { render :xml => @user }
    end
  end

but while the form will accept the data, nothing is populating the
foreign keys. In other words, I end up with unassociated records in the
database, when I'm actually expecting the records of the nested form to
be correctly linked.

What am I doing wrong here?

--
"Oh, look: rocks!"
        -- Doctor Who, "Destiny of the Daleks"


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Foreign keys not being saved by nested forms

by Frederick Cheung-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Nov 3, 9:39 pm, "Todd A. Jacobs" <tjacobs-sndr-
b4f...@...> wrote:

>
> but while the form will accept the data, nothing is populating the
> foreign keys. In other words, I end up with unassociated records in the
> database, when I'm actually expecting the records of the nested form to
> be correctly linked.

Your associations are back to front - users has a contact_id column,
so user belongs_to contact, not has_one (ditto contacts and
address_id)

Fred
>
> What am I doing wrong here?
>
> --
> "Oh, look: rocks!"
>         -- Doctor Who, "Destiny of the Daleks"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Foreign keys not being saved by nested forms

by Todd A. Jacobs-20 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Tue, Nov 03, 2009 at 02:06:32PM -0800, Frederick Cheung wrote:

> Your associations are back to front - users has a contact_id column,
> so user belongs_to contact, not has_one (ditto contacts and
> address_id)

Thanks, that fixed it. I think my difficulty is that, while has_many
generally makes sense semantically, belongs_to doesn't always. As long
as I think of it as "has_foreign_key_for" instead, it all works out
fine.

For example, in my case a contact has a foreign key to an address. The
address "belongs to" the contact (not the other way around), but I need
to use belongs_to in the model to build the proper relationship.

Thanks again for the help.

--
"Oh, look: rocks!"
        -- Doctor Who, "Destiny of the Daleks"


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Foreign keys not being saved by nested forms

by xds2000 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi,same here,i cant resove this problem.But i found a demo ,that's awesome.

http://github.com/anathematic/has_one_problem


2009/11/4 Todd A. Jacobs <tjacobs-sndr-b4faac@...>

For example, in my case a contact has a foreign key to an address. The
address "belongs to" the contact (not the other way around), but I need
to use belongs_to in the model to build the proper relationship.

Thanks again for the help.


--
tommy xiao
E-mail: xiaods(AT)gmail.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---