Relationship Like Twitter Followers/Followed in Activerecord

Relationship like Twitter followers/followed in ActiveRecord

You need two models, a Person and a Followings

rails generate model Person name:string
rails generate model Followings person_id:integer follower_id:integer blocked:boolean

and the following code in the models

class Person < ActiveRecord::Base
has_many :followers, :class_name => 'Followings', :foreign_key => 'person_id'
has_many :following, :class_name => 'Followings', :foreign_key => 'follower_id'
end

and corresponding in the Followings class you write

class Followings < ActiveRecord::Base
belongs_to :person
belongs_to :follower, :class_name => 'Person'
end

You could make the names clearer to your liking (i especially don't like the Followings-name), but this should get you started.

Rails: how to represent user follow user relationship?

This should be what you're looking for: http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#cha-following_users

For a more expansive friendship model where users can request to be friends, show pending friends etc. you could try this gem https://github.com/raw1z/amistad and this is a really good tutorial on how to get it working: http://keighl.com/post/amistad-friendships-controller

Hope that helps!

Ruby on Rails - Users following relationship

You are missing the following:

  • has_many :followings in the User class.

Class definitions:

class User < ActiveRecord::Base
...
has_many :followings
has_many :followers, through: :followings
...
end

class class Following < ActiveRecord::Base
belongs_to :user
belongs_to :follower, class_name: 'User'
end

After running the migration to create the followings table, you should be able to do the following from the console:

> jerry = User.create( :name => "jerry", :email => "jerry@seinfeld.com", :password => "newman" )
> george = User.create( :name => "george", :email => "george@costanza.com", :password => "bosco" )
> elaine = User.create( :name => "elaine", :email => "elaine@benes.com", :password => "dance" )
> kramer = User.create( :name => "kramer", :email => "cozmo@kramer.com", :password => "bagels" )

> kramer.followers
=> #<ActiveRecord::Associations::CollectionProxy []>
> kramer.followers.count
=> 0
> kramer.followers << george

> kramer.followers.count
=> 1
> kramer.followers.map(&:name)
> ["george"]

Creating a follow/unfollow relationship from scratch between two models in Rails

You are looking for a "many to many" relationship. You can find more detailed information about this relation here. With your models as examples it would look like this:

class User < ActiveRecord::Base
has_many :followings, :dependent => :destroy
has_many :celebrities, :through => :following
end

class Following < ActiveRecord::Base
belongs_to :user
belongs_to :celebrity
end

class Celebrity < ActiveRecord::Base
has_many :followings, :dependent => :destroy
has_many :users, :through => :following
end

Ruby on Rails has_many through self-referential following/follower relationships

You need to make sure ActiveRecord knows what the source association for the User#friends and likewise the followers and specify the class and foreign_key for the relationships that ActiveRecord can't extrapolate from the association names.

class Following < ActiveRecord::Base

belongs_to :user
belongs_to :followed, :class_name => 'User'

end

class User < ActiveRecord::Base

has_many :followings
has_many :friends, :through => :followings, :source => 'followed'

has_many :followeds, :class_name => 'Following', :foreign_key => 'followed_id'
has_many :followers, :through => :followeds, :source => :user

end


Related Topics



Leave a reply



Submit