Skip to content

Instantly share code, notes, and snippets.

@nealfennimore
Created May 6, 2014 20:07
Show Gist options
  • Select an option

  • Save nealfennimore/e3045af953bc30c241ab to your computer and use it in GitHub Desktop.

Select an option

Save nealfennimore/e3045af953bc30c241ab to your computer and use it in GitHub Desktop.
ActiveRecord
# 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