SlideShare a Scribd company logo
Active Record Association
Self-Join
Ror lab.
- Speicial episode -
August 1st, 2013
Hyoseong Choi
User Model
class User < ActiveRecord::Base
has_and_belongs_to_many :friends,
class_name: 'User',
foreign_key: 'friend_id',
association_foreign_key: 'user_id',
join_table: 'users_friends'
def is_friend_of?(user_id)
friend_ids.include? user_id
end
end
‘users’ table
$ rails g model User name
$ rake db:migrate
== CreateUsers: migrating ===============================
-- create_table(:users)
-> 0.0062s
== CreateUsers: migrated (0.0062s) ======================
‘users_friends’ table
$ rails g migration create_join_table user:references
friend:references
class CreateJoinTable < ActiveRecord::Migration
def change
create_table :users_friends do | t |
t.references :user, index: true
t.references :friend, index: true
t.timestamps
end
end
end
$ rake db:migrate
== CreateJoinTable: migrating ===============================
-- create_table(:users_friends)
-> 0.0035s
== CreateJoinTable: migrated (0.0035s) ======================
User Friend
UsersFriend
class User < ActiveRecord::Base
has_and_belongs_to_many :friends
end
class Friend < ActiveRecord::Base
has_and_belongs_to_many :users
end
class UsersFriend < ActiveRecord::Base
belongs_to :user
belongs_to :friend
end
has_and_belongs_to_many
User User
UsersFriend
class UsersFriend < ActiveRecord::Base
belongs_to :user
belongs_to :friend
end
has_and_belongs_to_many
class User < ActiveRecord::Base
has_and_belongs_to_many :friends,
class_name: ‘User’,
foreign_key: ‘friend_id’
association_foreign_key: ‘user_id’
end
self join
감사합니다.

More Related Content

DOCX
Karan - form search
PPTX
PDF
Short intro to JQuery and Modernizr
PPTX
Css Selectors
PDF
J queryui
PPT
Hi5 Opensocial Code Lab Presentation
KEY
Datamapper @ Railsconf2010
Karan - form search
Short intro to JQuery and Modernizr
Css Selectors
J queryui
Hi5 Opensocial Code Lab Presentation
Datamapper @ Railsconf2010

What's hot (19)

PPTX
21. CodeIgniter search
PDF
JQuery UI
DOCX
PDF
Jquery In Rails
PPT
Mysocial databasequeries
PPT
Mysocial databasequeries
PDF
Introducing jQuery
PPTX
Mootools selectors
PPT
Don't Worry jQuery is very Easy:Learning Tips For jQuery
KEY
Sakai customization talk
PDF
Game jump: frontend introduction #1
PDF
Jquery for post a form
PPTX
Sins Against Drupal 2
PDF
Javascript session june 2013 (iii) jquery json
PPTX
Drupal sins 2016 10-06
PPTX
17. CodeIgniter login simplu cu sesiuni
PPT
Intro to jQuery
PPTX
Select This! Entity Selectors in Reason CMS
21. CodeIgniter search
JQuery UI
Jquery In Rails
Mysocial databasequeries
Mysocial databasequeries
Introducing jQuery
Mootools selectors
Don't Worry jQuery is very Easy:Learning Tips For jQuery
Sakai customization talk
Game jump: frontend introduction #1
Jquery for post a form
Sins Against Drupal 2
Javascript session june 2013 (iii) jquery json
Drupal sins 2016 10-06
17. CodeIgniter login simplu cu sesiuni
Intro to jQuery
Select This! Entity Selectors in Reason CMS
Ad

Similar to Self join in active record association (18)

PDF
Where's My SQL? Designing Databases with ActiveRecord Migrations
PDF
Rails DB migrations
PDF
Association in rails
ODP
Ruby on rails
PDF
Introduction to Active Record at MySQL Conference 2007
KEY
Rails Model Basics
PDF
Effective ActiveRecord
PPTX
Active record(1)
PPT
Intro to Rails ActiveRecord
PDF
Migrations In3 Mins
PDF
Introduction to Active Record - Silicon Valley Ruby Conference 2007
PDF
Desenvolvimento web com Ruby on Rails (parte 5)
PPTX
Ruby on Rails with Active Record
KEY
Rails Database Migration, Season 2
DOC
How to implement “multiple database(db) connection” in rails3
KEY
Active Record Query Interface (2), Season 1
PDF
Ruby meetup ROM
PPTX
associations.pptx
Where's My SQL? Designing Databases with ActiveRecord Migrations
Rails DB migrations
Association in rails
Ruby on rails
Introduction to Active Record at MySQL Conference 2007
Rails Model Basics
Effective ActiveRecord
Active record(1)
Intro to Rails ActiveRecord
Migrations In3 Mins
Introduction to Active Record - Silicon Valley Ruby Conference 2007
Desenvolvimento web com Ruby on Rails (parte 5)
Ruby on Rails with Active Record
Rails Database Migration, Season 2
How to implement “multiple database(db) connection” in rails3
Active Record Query Interface (2), Season 1
Ruby meetup ROM
associations.pptx
Ad

More from RORLAB (20)

PDF
Getting Started with Rails (4)
PDF
Getting Started with Rails (3)
PDF
Getting Started with Rails (2)
PDF
Getting Started with Rails (1)
PDF
Asset Pipeline in Ruby on Rails
PDF
레일스가이드 한글번역 공개프로젝트 RORLabGuides 소개
PDF
Active Support Core Extension (3)
PDF
Active Support Core Extension (2)
PDF
Active Support Core Extensions (1)
PDF
Action Controller Overview, Season 2
PDF
Action View Form Helpers - 2, Season 2
PDF
Action View Form Helpers - 1, Season 2
PDF
Layouts and Rendering in Rails, Season 2
PDF
ActiveRecord Query Interface (2), Season 2
KEY
Active Record Query Interface (1), Season 2
KEY
Active Record Association (2), Season 2
KEY
ActiveRecord Association (1), Season 2
KEY
ActiveRecord Callbacks & Observers, Season 2
KEY
ActiveRecord Validations, Season 2
KEY
Getting started with Rails (4), Season 2
Getting Started with Rails (4)
Getting Started with Rails (3)
Getting Started with Rails (2)
Getting Started with Rails (1)
Asset Pipeline in Ruby on Rails
레일스가이드 한글번역 공개프로젝트 RORLabGuides 소개
Active Support Core Extension (3)
Active Support Core Extension (2)
Active Support Core Extensions (1)
Action Controller Overview, Season 2
Action View Form Helpers - 2, Season 2
Action View Form Helpers - 1, Season 2
Layouts and Rendering in Rails, Season 2
ActiveRecord Query Interface (2), Season 2
Active Record Query Interface (1), Season 2
Active Record Association (2), Season 2
ActiveRecord Association (1), Season 2
ActiveRecord Callbacks & Observers, Season 2
ActiveRecord Validations, Season 2
Getting started with Rails (4), Season 2

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Business Ethics Teaching Materials for college
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
PDF
Complications of Minimal Access Surgery at WLH
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Renaissance Architecture: A Journey from Faith to Humanism
Business Ethics Teaching Materials for college
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPH.pptx obstetrics and gynecology in nursing
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Basic Mud Logging Guide for educational purpose
Complications of Minimal Access Surgery at WLH
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Anesthesia in Laparoscopic Surgery in India

Self join in active record association

  • 1. Active Record Association Self-Join Ror lab. - Speicial episode - August 1st, 2013 Hyoseong Choi
  • 2. User Model class User < ActiveRecord::Base has_and_belongs_to_many :friends, class_name: 'User', foreign_key: 'friend_id', association_foreign_key: 'user_id', join_table: 'users_friends' def is_friend_of?(user_id) friend_ids.include? user_id end end
  • 3. ‘users’ table $ rails g model User name $ rake db:migrate == CreateUsers: migrating =============================== -- create_table(:users) -> 0.0062s == CreateUsers: migrated (0.0062s) ======================
  • 4. ‘users_friends’ table $ rails g migration create_join_table user:references friend:references class CreateJoinTable < ActiveRecord::Migration def change create_table :users_friends do | t | t.references :user, index: true t.references :friend, index: true t.timestamps end end end $ rake db:migrate == CreateJoinTable: migrating =============================== -- create_table(:users_friends) -> 0.0035s == CreateJoinTable: migrated (0.0035s) ======================
  • 5. User Friend UsersFriend class User < ActiveRecord::Base has_and_belongs_to_many :friends end class Friend < ActiveRecord::Base has_and_belongs_to_many :users end class UsersFriend < ActiveRecord::Base belongs_to :user belongs_to :friend end has_and_belongs_to_many
  • 6. User User UsersFriend class UsersFriend < ActiveRecord::Base belongs_to :user belongs_to :friend end has_and_belongs_to_many class User < ActiveRecord::Base has_and_belongs_to_many :friends, class_name: ‘User’, foreign_key: ‘friend_id’ association_foreign_key: ‘user_id’ end self join
  • 8.