Get the user id of a newly created user and put it into nested form

78 views Asked by At

I had no clue how to best phrase this. If anyone thinks they can do a better job be my guest :)

I am trying to do something somewhat difficult. I have a devise model called User and a User has many Addresses. When a user signs up I want them to have to put in an address and have the address get added to the database with the user_id of the current user.

Right now I am trying to accomplish this with cocoon, but I can't figure out how to make it create the user first, and then create the address with the user_id of the user that was just created.

This is what my form looks like if it helps:

Screenshot of form

Here's the user and address parts of schema.rb

  create_table "addresses", force: :cascade do |t|
    t.integer  "user_id"
    t.string   "line_one",     default: "",            null: false
    t.string   "line_two",     default: ""
    t.integer  "apartment"
    t.string   "city",                                 null: false
    t.string   "state",                                null: false
    t.integer  "zip",                                  null: false
    t.string   "address_type", default: "residential", null: false
    t.boolean  "default",      default: true,          null: false
    t.datetime "created_at",                           null: false
    t.datetime "updated_at",                           null: false
  end

  add_index "addresses", ["user_id"], name: "index_addresses_on_user_id", using: :btree

  create_table "users", force: :cascade do |t|
    t.string   "username",               default: "", null: false
    t.string   "email",                  default: "", null: false
    t.string   "first_name",             default: "", null: false
    t.string   "middle_initial",         default: ""
    t.string   "last_name",              default: "", null: false
    t.integer  "profile_pic_id",         default: 0
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

User model:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :username, presence: true, uniqueness: { case_sensitive: false }

  attr_accessor :login

  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions.to_h).first
    end
  end

  has_many :auctions, dependent: :destroy
  has_many :addresses, dependent: :destroy
end

Address model:

class Address < ActiveRecord::Base
  belongs_to :user
end

Let me know if you need anything else!

1

There are 1 answers

0
BinaryMee On

Have a look at Nested Attributes.

Using accepts_nested_attributes_for you could save associated record through the parent.
So, in your case you could save multiple address associated with User record along with User.

For example, User model would be like

class User < ActiveRecord::Base
  has_many :addresses, dependent: :destroy
  accepts_nested_attributes_for :addresses
end 

Then on creating records you could save address records along with the User record.

user_attr = {
  username: 'mascaliente', addresses_attributes: [
    { line_one: 'Line1', city: 'City1',state: 'State1', zip: 'Zip1' },
    { line_one: 'Line2', city: 'City2',state: 'State2', zip: 'Zip2' }
  ]
}
user = User.create(user_attr)