Skip to content

Instantly share code, notes, and snippets.

@fonsecajavier
Created November 29, 2012 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fonsecajavier/4169727 to your computer and use it in GitHub Desktop.
Save fonsecajavier/4169727 to your computer and use it in GitHub Desktop.
Model schemas and relationships for "follower" and "followed" users
# == Schema Information
#
# Table name: events
#
# id :integer not null, primary key
# title :string(255)
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Event < ActiveRecord::Base
belongs_to :user
end
# == Schema Information
#
# Table name: follow_up_relationships
#
# id :integer not null, primary key
# follower_id :integer
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class FollowUpRelationship < ActiveRecord::Base
belongs_to :user
belongs_to :follower, :class_name => "User"
end
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
attr_accessible :name
validates_presence_of :name
has_many :follower_relationships, :class_name => "FollowUpRelationship"
has_many :followers, :through => :follower_relationships
has_many :followed_relationships, :class_name => "FollowUpRelationship", :foreign_key => :follower_id
has_many :followeds, :through => :followed_relationships, :source => :user
has_many :events
def followed_events
Event.where(:user_id => followeds)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment