Self-Referential Association In Rails

rails g resource friendship user_id:integer friend_id:integer
belongs_to :user
belongs_to :friend, :class_name => “User”
has_many :friendships
has_many :friends, :through => :friendships
def create
Friendship.create(
user_id: params[:user_id],
friend_id: params[friend_id]
)
end

--

--

--

Student at The Flatiron School in Washington DC

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Getting To Know About React JS

Discord.ts Guards

How to Optimize a React Native App for Better Performance?

How to add JavaScript linting to your project using ESLint and Gulp

What is event in JavaScript?

GSOC 2018 : My Summer with Processing Foundation

OTP Smasher — H@cktivityCon 2021 CTF

6 super useful libraries for React and React Native

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Adolfo

Adam Adolfo

Student at The Flatiron School in Washington DC

More from Medium

Rails CRUD

Rails Project Reflection — A Ruby on Rails Recipe Manager

Learn Ruby on Rails API

Sinatra or Rails: Ruby Backend Frameworks

Ruby Frameworks: Sinatra vs. Rails