Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User.first.friends
- User.first.friendships
- +---------+-----------+
- | user_id | friend_id |
- +---------+-----------+
- | 1 | 2 |
- | 1 | 3 |
- +---------+-----------+
- create_table "friendships", id: false, force: :cascade do |t|
- t.integer "user_id"
- t.integer "friend_id"
- end
- create_table "users", force: :cascade do |t|
- t.string "name"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- end
- class Friendship < ApplicationRecord
- belongs_to :user, foreign_key: "user_id", class_name: "User"
- belongs_to :friend, foreign_key: "friend_id", class_name: "User"
- end
- class User < ApplicationRecord
- has_many :friendships, foreign_key: "user_id", class_name: "Friendship"
- has_many :friends, through: :friendships
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement