Created
November 29, 2012 15:15
-
-
Save fonsecajavier/4169727 to your computer and use it in GitHub Desktop.
Model schemas and relationships for "follower" and "followed" users
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
# == 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 |
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
# == 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 |
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
# == 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