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 theUser
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
Where to Put Ruby Helper Methods for Rails Controllers
How to Install Jekyll on Osx 10.11
Openssl::Ssl::Sslerror: Ssl_Connect Returned=1 Errno=0 State=Unknown State: Unknown Protocol
Why Many People Use "-%>" Instead of "%>" in Rails
Convert Time to Other Timezone
Where to Define Custom Error Types in Ruby And/Or Rails
Understanding Ruby .Class and .Ancestors Methods
In Rails, Display Time Between Two Dates in English
Ruby on Rails: Can You Put Ruby Code in a Yaml Config File
What Does 'Def Self.Function' Name Mean
What Does the "$" Character Mean in Ruby
How to Use Hash Keys as Methods on a Class
Prevent Rails Test from Deleting Seed Data
Rails, Postgresql, and History Triggers
Docker for MAC - Mkmf.Rb Can't Find Header Files for Ruby
In Ruby's Test::Unit::Testcase, How to Override the Initialize Method