Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

Many to Many Active Record

# id    email   encrypted_password
class User < ActiveRecord::Base
    has_many :tweets
    has_many :likes
    has_many :follows_as_follower, class_name: 'Follow', 
  foreign_key: :follower_user_id
    has_many :follows_as_followed, class_name: 'Follow', 
  foreign_key: :followed_user_id
    has_many :followed_users, through: :follows_as_follower, 
  source: :followed_user
    has_many :followers, through: :follows_as_followed, 
  source: :follower_user
end
# id content user_id
class Tweet < ActiveRecord::Base
    belongs_to :user
    has_many :likes
end

# id follower_user_id followed_user_id
class Follow < ActiveRecord::Base
    belongs_to :follower_user, class_name: 'User'
    belongs_to :followed_user, class_name: 'User'
end

# id user_id tweet_id
class Like < ActiveRecord::Base
    belongs_to :user
    belongs_to :tweet
end
Source by p3-06-activerecord-associations-many-to-many-slides.netlify.app #
 
PREVIOUS NEXT
Tagged: #Many #Many #Active #Record
ADD COMMENT
Topic
Name
6+8 =