Created
May 6, 2014 20:07
-
-
Save nealfennimore/e3045af953bc30c241ab to your computer and use it in GitHub Desktop.
ActiveRecord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Study has_many, belongs_to methods (10 or so) | |
| ActiveRecord::Migration.create_table :followings do |t| | |
| t.belongs :follower | |
| t.belong :stalked | |
| end | |
| ActiveRecord::Migrator.up "db/migrate" | |
| class User < ActiveRecord::Base | |
| # I follow people. | |
| has_many :followings, foreign_key: "follower_id" | |
| has_many: :stalked_users, through: :followings, source: :stalked | |
| # People follow me. | |
| has_many :opposite_followings, class_name: "Following", foreign_key: "stalked_id" | |
| has_many :followers, through: :opposite_followings, source: :follower | |
| def follow(user) | |
| stalked_users << user | |
| end | |
| def stop_followin! | |
| stalked_users.clear | |
| end | |
| def so_lonely? | |
| end | |
| end | |
| class Following < ActiveRecord::Base | |
| belongs_to :follower, class_name: "User" | |
| belongs_to :stalked, class_name: "User" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment