From eecdecfda966c6e923db4dc1f63a127e613da976 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sun, 30 Nov 2014 14:43:35 +0100 Subject: [PATCH] added validations and everything --- app/models/relationship.rb | 4 ++++ app/models/user.rb | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/relationship.rb b/app/models/relationship.rb index 210bc921..20dec636 100644 --- a/app/models/relationship.rb +++ b/app/models/relationship.rb @@ -1,2 +1,6 @@ class Relationship < ActiveRecord::Base + belongs_to :source, class_name: 'User' + belongs_to :target, class_name: 'User' + validates :source_id, presence: true + validates :target_id, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 9cd79bec..3858c8a2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,7 +11,15 @@ class User < ActiveRecord::Base has_many :answers, dependent: :destroy has_many :comments, dependent: :destroy has_many :inboxes, dependent: :destroy - + has_many :active_relationships, class_name: 'Relationship', + foreign_key: 'source_id', + dependent: :destroy + has_many :passive_relationships, class_name: 'Relationship', + foreign_key: 'target_id', + dependent: :destroy + has_many :friends, through: :active_relationships, source: :target + has_many :followers, through: :passive_relationships, source: :source + SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }