I've been trying to implement a Facebook style friend system. I've read several tutorials and can't understand what I'm doing wrong. The friendship controller and the links in the views for adding friends work, but on the database level it throws an error.
When I open the rails console and put:
User.find(1).friends << User.find(2)
I get:
SQL (0.4ms) INSERT INTO "friendships" ("user_id", "friend_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 1], ["friend_id", 2], ["created_at", "2017-10-14 15:01:28.364691"], ["updated_at", "2017-10-14 15:01:28.364691"]]
(0.1ms) rollback transaction
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.friends: INSERT INTO "friendships" ("user_id", "friend_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)
from (irb):1
user.rb
has_many :friendships, dependent: :destroy
has_many :friends, through: :friendships
# these are used in the controller
def befriend(user)
friends << user
end
def unfriend(user)
friends.delete(user)
end
def friends?(user)
friends.include?(user)
end
friendship.rb
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: "friend_id"
validates :user_id, presence: true
validates :friend_id, presence: true
I'm using Devise for authentication.