Episodes
Zizo said almost 4 years ago on Friend Requests From Scratch :
Great episode Dave! Looking forward to watching the next one.

David Kimura PRO said about 2 years ago on Friend Requests From Scratch :
  can you post the migration files that you're trying to run?

hwy694 said about 2 years ago on Friend Requests From Scratch :
  Sure, let me know if this helps: 

rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "friends" does not exist
/Users/luke/Desktop/Actualize/soundjek-v-2/soundjek-backend/db/migrate/20200827013143_create_friendships.rb:3:in `change'
bin/rails:4:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "friends" does not exist
/Users/luke/Desktop/Actualize/soundjek-v-2/soundjek-backend/db/migrate/20200827013143_create_friendships.rb:3:in `change'
bin/rails:4:in `<main>'

Caused by:
PG::UndefinedTable: ERROR:  relation "friends" does not exist
/Users/luke/Desktop/Actualize/soundjek-v-2/soundjek-backend/db/migrate/20200827013143_create_friendships.rb:3:in `change'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate




marketing said about 2 years ago on Friend Requests From Scratch :
You need to edit the migration before you run it to add the references to the User table:

class CreateFriendships < ActiveRecord::Migration[6.0]
  def change
    create_table :friendships do |t|
      t.references :user, null: false, foreign_key: { to_table: :users }
      t.references :friend, null: false, foreign_key: { to_table: :users }
      t.integer :status, default: 0, limit: 1

      t.timestamps
    end
  end
end


liam said over 1 year ago on Friend Requests From Scratch :
I'm having trouble with being able to create a friendship (which I've named a 'connection') in the rails console, so that users can be friends (which I've named 'contacts'):

2.7.0 :033 > user4.contact_request(user5)
  Connection Exists? (0.3ms)  SELECT 1 AS one FROM "connections" WHERE "connections"."user_id" = ? AND "connections"."contact_id" = ? LIMIT ?  [["user_id", 7], ["contact_id", 8], ["LIMIT", 1]]
  TRANSACTION (0.1ms)  begin transaction
  Connection Create (1.1ms)  INSERT INTO "connections" ("user_id", "contact_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["user_id", 7], ["contact_id", 8], ["created_at", "2021-04-19 23:45:32.553041"], ["updated_at", "2021-04-19 23:45:32.553041"]]
  TRANSACTION (0.1ms)  rollback transaction
Traceback (most recent call last):
        4: from (irb):32
        3: from (irb):33:in `rescue in irb_binding'
        2: from app/models/user.rb:49:in `contact_request'
        1: from app/models/user.rb:50:in `block in contact_request'
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.contacts)

I'm not sure what to do here!

David Kimura PRO said over 1 year ago on Friend Requests From Scratch :
  Can you share your user model with the relevant bits and the connection model?

liam said over 1 year ago on Friend Requests From Scratch :
Hey David, sure thing.

User model: 
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
	has_many :developments, dependent: :destroy
	has_many :connections
	has_many :connections, dependent: :destroy
	has_many :contacts, -> { where connections: { status: :accepted }}, through: :connections
	has_many :requested_contacts, -> { where connections: {status: :requested}}, through: :connections, source: :contact
	has_many :pending_contacts, -> { where connections: { status: :pending }}, through: :connections, source: :contact	
        has_many :blocked_contacts, -> { where connections: { status: :blocked }}, through: :connections, source: :contact

and the Connection model:
class Connection < ApplicationRecord
  belongs_to :user
  belongs_to :contact, class_name: 'User'
  enum status: {pending: 0, requested: 1, accepted: 2, blocked: 3}
  # id: 1
  # contact: 2

  # user_id: 1, contact_id: 2, status: :requested
  # user_id: 2, contact_id: 1, status: :pending
end

I've tried double and triple-checking my code to make sure it's the same as yours but I can't quite figure it out!

David Kimura PRO said over 1 year ago on Friend Requests From Scratch :
Can you also share the contact_request and accept_request methods?

David Kimura PRO said over 1 year ago on Friend Requests From Scratch :
➜  155-friend-requests-from-scratch-master bin/rails c
Loading development environment (Rails 5.2.5)
2.7.3 :001 > user = User.first
  User Load (0.1ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
 => #<User id: 1, email: "[email protected]", created_at: "2021-04-20 01:30:35", updated_at: "2021-04-20 01:30:35"> 
2.7.3 :002 > user2 = User.last
  User Load (0.3ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ?  [["LIMIT", 1]]
 => #<User id: 4, email: "[email protected]", created_at: "2021-04-20 01:30:35", updated_at: "2021-04-20 01:30:35"> 
2.7.3 :003 > user.friend_request(user2)
  Friendship Exists (0.1ms)  SELECT  1 AS one FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ?  [["user_id", 1], ["friend_id", 4], ["LIMIT", 1]]
   (0.0ms)  begin transaction
  Friendship Create (0.6ms)  INSERT INTO "friendships" ("user_id", "friend_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["user_id", 1], ["friend_id", 4], ["created_at", "2021-04-20 01:30:59.662695"], ["updated_at", "2021-04-20 01:30:59.662695"]]
  Friendship Create (0.1ms)  INSERT INTO "friendships" ("user_id", "friend_id", "status", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["user_id", 4], ["friend_id", 1], ["status", 1], ["created_at", "2021-04-20 01:30:59.664399"], ["updated_at", "2021-04-20 01:30:59.664399"]]
   (0.7ms)  commit transaction
 => #<Friendship id: 2, user_id: 4, friend_id: 1, status: "requested", created_at: "2021-04-20 01:30:59", updated_at: "2021-04-20 01:30:59"> 
2.7.3 :004 > 

I downloaded the source and got it running and it still seems to work okay.

You do have two "has_many :connections" defined in the user model which is a bit odd.


liam said over 1 year ago on Friend Requests From Scratch :

def contact_request(contact)
		unless self == contact || Connection.where(user: self, contact: contact).exists?
			transaction do
			Connection.create(user: self, contact: contact, status: :pending)
			Connection.create(user: contact, contact: self, status: :requested)
			end
		end
	end

	def accept_request(contact)
		transaction do
			Connection.find_by(user: self, contact: contact, status: [:requested])&.accepted!
			Connection.find_by(user: contact, contact: self, status: [:pending])&.accepted!
		end
	end

Odd that you're able to get it working... would it make any difference that I've created my app with PostgresQL and not SQLite?

David Kimura PRO said over 1 year ago on Friend Requests From Scratch :
In the episode, I was using mysql, so I don't think that's the issue. Are you by chance using Spring? Maybe bin/spring stop and restart your application to see if that helps.



liam said over 1 year ago on Friend Requests From Scratch :
Ah okay, that's good to know. Tried stopping Spring, still getting the same error in the console

liam said over 1 year ago on Friend Requests From Scratch :
 Here's my relevant schema code:
create_table "connections", force: :cascade do |t|
    t.integer "user_id", null: false
    t.integer "contact_id", null: false
    t.integer "status", limit: 1, default: 0
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["contact_id"], name: "index_connections_on_contact_id"
    t.index ["user_id"], name: "index_connections_on_user_id"
  end

create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.boolean "admin", default: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "connections", "contacts"
  add_foreign_key "connections", "users"
end

and the connections migration, if it helps:

class CreateConnections < ActiveRecord::Migration[6.1]
  def change
    create_table :connections do |t|
      t.references :user, null: false, foreign_key: { to_table: :users}
      t.references :contact, null: false, foreign_key: {to_table: :users}
      t.integer :status, default: 0, limit: 1

      t.timestamps
    end
  end
end

I tried to edit the migration as above based on comments made by marketing. I just directly edited the file, I wasn't sure if I needed to run a new migration. From what I've read on stack overflow and other places, the error I'm getting seems to be something to do with the migrations.

David Kimura PRO said over 1 year ago on Friend Requests From Scratch :
  if you've not deployed the application yet then you can roll back the database migrations first and then make the changes. If you have already deployed the application then things get a bit trickier because you've already gotten data populated on those columns. If you've already deployed the application or have team mates and the code has already been merged then I would create another migration.

liam said over 1 year ago on Friend Requests From Scratch :
 But are the migration changes necessary from what you can see? Or do I need to send you through some more code? The project is deployed on GitHub but it's only a dummy app to test code before making larger, nice changes in the real repository (I know it seems unnecessary but it helps give a lot more room for error).

David Kimura PRO said over 1 year ago on Friend Requests From Scratch :
I could have a look this week. I'm not sure if there could be something that's changed with the Rails version or of there is some other hiccup that could be going on.

liam said over 1 year ago on Friend Requests From Scratch :
Hi   , just wondering if you had a chance to take a look at it this week? I'm still stuck! I made a stack overflow post that has had no traction yet:
https://stackoverflow.com/questions/67171189/activerecordstatementinvalid-sqlite3sqlexception-no-such-table-main-conta

I'll have a bit more of a sift through all the code that you have in the other tab here to see if there are any other major discrepancies. 

David Kimura PRO said over 1 year ago on Friend Requests From Scratch :
  I didn't see a link to the code base. Did you share it. 

Login to Comment