0

Ok so I have a brand new rails 3.2.3 application that i need users with lots of other attributes like genre and birthday and others. I am asking for the best way to structure the db. I was thinking users and profiles

class Profile < ActiveRecord::Base
  belongs_to :contact

class User < ActiveRecord::Base
  has_one :profile

and putting the first and last name and password info in the users table and the other data in the profiles table. I am using devise as the the user authentication. But i have a signup form that has all the info needed, so is there a an easy way to avoid the nesting of the two tables in the form. Is there an easy way to do this...this seems very basic but has me a bit confused

Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321

1 Answers1

1

Personally, I like to have data that point to the same entity in the same table,

User => name, address, password, ... #use role_id for different roles ie admin, tester, viewer? Depends what you need and how complex it is.

You want to use belongs_to => :user #module\table name

Your form will need to be updated to get the profile data like user.profile.password ... (this is the easy way from what I understood from your question)

When you want to edit you can use something like edit_user_profile_path(current_user, @profile) notice the path user_profile, this is where it knows from where to grab it.

like in this answer https://stackoverflow.com/a/4753856/643500


On a side note, if you are not proficient with rails, consider writing out your first authentication system from scratch. This way you will learn where everything goes and be able to know how to customize it, or ones like devise. Tutorial here http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Community
  • 1
  • 1
Sully
  • 14,672
  • 5
  • 54
  • 79