\d users
Table "public.users"
Column | Type | Modifiers
------------------------+-----------------------------+----------------------------------------------------
id | integer | not null default nextval('users_id_seq'::regclass)
email | character varying(255) | not null default ''::character varying
encrypted_password | character varying(128) | not null default ''::character varying
reset_password_token | character varying(255) |
reset_password_sent_at | timestamp without time zone |
remember_created_at | timestamp without time zone |
sign_in_count | integer | default 0
current_sign_in_at | timestamp without time zone |
last_sign_in_at | timestamp without time zone |
current_sign_in_ip | character varying(255) |
last_sign_in_ip | character varying(255) |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
"index_users_on_email" UNIQUE, btree (email)
"index_users_on_reset_password_token" UNIQUE, btree (reset_password_token)
\d posts
Table "public.posts"
Column | Type | Modifiers
-------------+------------------------+----------------------------------------------------
id | integer | not null default nextval('posts_id_seq'::regclass)
title | character varying(100) | not null
content | character varying(500) | not null
created_at | date |
updated_at | date |
tags | character varying(55) | not null default '50'::character varying
category_id | integer | not null default 1
Indexes:
"posts_pkey" PRIMARY KEY, btree (id)
I need to keep track who is post a post, so i need username column in posts table.
Shoud i add a username column in posts table or should i add username in users table or should i create a different table for userdetails with username.
I also need to check whether the user is admin or normal user.
Can anyone post complete relational database schema for this purpose ?
devise User model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
You don’t want a username in your
poststable, you just want the user’s ID. The username would go in youruserstable.For your
userstable, you’d want this in the table creation:and a
:limitif you want to limit the length. Then in yourUserclass:If you don’t want usernames to be unique then you could drop the
validates :username; you could also downcase the usernames in abefore_validatehook and add a unique index onusernameto youruserstable. You might want some flags on thehas_many, see the guide for details.Then, when creating
posts:and in
Post:Then you can get the username for a post through its
user:If you’re trying to add usernames to existing data then you’d need
:null => truein your migrations for the new columns and you’d have to figure out what you want to do with the missing values in your existing data.I don’t know how any of this would integrate with devise (sorry, I don’t use it) or if devise will even care about usernames. Hopefully someone else can fill in the devise parts (if any) and leave me a comment if they do.