Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

User Profile with devise

Gems / Libraries • Asked by Darren Nickerson

Hello I'm new to rails and I am trying to work with the has_one association. I'm using Devise for authentication. I was each user to have a profile (which will include their address and some other information that I may need later on. I thought about creating a migration to add these to the user model but thought it would make it bloated (let me know if this is a correct way of thinking).

For the association I have:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
after_create :create_profile
has_one :profile
end
class Profile < ApplicationRecord
  belongs_to :user
end

I'm also using nested attributes for this so my routes look like this

user_profiles GET    /users/:user_id/profiles(.:format)                          profiles#index
                          POST   /users/:user_id/profiles(.:format)                          profiles#create
         new_user_profile GET    /users/:user_id/profiles/new(.:format)                      profiles#new
        edit_user_profile GET    /users/:user_id/profiles/:id/edit(.:format)                 profiles#edit
             user_profile GET    /users/:user_id/profiles/:id(.:format)                      profiles#show
                          PATCH  /users/:user_id/profiles/:id(.:format)                      profiles#update
                          PUT    /users/:user_id/profiles/:id(.:format)                      profiles#update
                          DELETE /users/:user_id/profiles/:id(.:format)                      profiles#destroy

So if I have to have a profile :id how am I suppose to figure out what the id will be. It will be different for each user since it increments for each profile that's created. The solution is probably obvious for most you guys out there but I've been banging my head against the wall for a bit trying to figure this out.

Thanks for any help :)


Hey Darren!

Normally I would recommend you just put them on the user model directly. The reasons to split it up are normally when you want a user to have multiple addresses (shipping and billing for example) and not so much worrying about a table being bloated. If you were talking 100 columns on one table, then sure that sounds bloated, but an address is no big deal.

On the other topic of routes, when you're doing a has_one you actually want to do a resource :profile instead of the plural resources :profiles because that will create routes like /profile instead and you don't need to specify the ID. Since there is only one profile per user, it doesn't make sense to pass the ID into the URL like you've probably noticed. :) A singular resource route will let you say @profile = current_user.profile without looking anything up by IDs, just associations.

I would probably say, start easy and just add them to the user model and add the fields to the edit registration page at the beginning. No real big sense in breaking out the profiles yet unless you have something that requires it to be split.


Thanks Chris! This cleared up a few different things for me. I appreciate the thorough answer :)


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.