Setup models for friends
This commit is contained in:
parent
34ec10189c
commit
1752dffb95
4 changed files with 25 additions and 1 deletions
4
app/models/friendship.rb
Normal file
4
app/models/friendship.rb
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
class Friendship < ActiveRecord::Base
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :friend, class_name: 'User'
|
||||||
|
end
|
|
@ -1,4 +1,7 @@
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
|
has_many :friendships
|
||||||
|
has_many :friends, through: :friendships
|
||||||
|
|
||||||
def self.from_omniauth(auth)
|
def self.from_omniauth(auth)
|
||||||
where(provider: auth[:provider], uid: auth[:uid]).first_or_create do |user|
|
where(provider: auth[:provider], uid: auth[:uid]).first_or_create do |user|
|
||||||
user.provider = auth[:provider]
|
user.provider = auth[:provider]
|
||||||
|
|
10
db/migrate/20141015144022_create_friendships.rb
Normal file
10
db/migrate/20141015144022_create_friendships.rb
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
class CreateFriendships < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
create_table :friendships do |t|
|
||||||
|
t.integer :user_id
|
||||||
|
t.integer :friend_id
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,14 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20141002202749) do
|
ActiveRecord::Schema.define(version: 20141015144022) do
|
||||||
|
|
||||||
|
create_table "friendships", force: true do |t|
|
||||||
|
t.integer "user_id"
|
||||||
|
t.integer "friend_id"
|
||||||
|
t.datetime "created_at"
|
||||||
|
t.datetime "updated_at"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "users", force: true do |t|
|
create_table "users", force: true do |t|
|
||||||
t.string "provider"
|
t.string "provider"
|
||||||
|
|
Loading…
Reference in a new issue