public
Last active

Model schemas and relationships for "follower" and "followed" users

  • Download Gist
event.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# == 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
follow_up_relationship.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# == 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
user.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# == 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.