0

Right now I'm trying to create a one-sided follow/unfollow relationship where a user can follow/unfollow a celebrity. Despite the gems available I'd like to learn how to create this from scratch.

The posts I've found online seem to only incorporate a self-referential style where a model instance can only follow one of its kind (a user can only follow other users). As a starting point point I've used this post for the initial setup. I'm just not sure how to re-configure the associations for models:

class User < ActiveRecord::Base
end

class Following < ActiveRecord::Base
end

class Celebrity < ActiveRecord::Base
end
Community
  • 1
  • 1
Carl Edwards
  • 13,826
  • 11
  • 57
  • 119

1 Answers1

0

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
Community
  • 1
  • 1
Mike S
  • 11,329
  • 6
  • 41
  • 76
  • 1
    I'd add `:dependent => :destroy` to both instances of `has_many :followings`, so that if a user or celebrity is destroyed, the joins to that record are also destroyed. Also, convention is to put `has_many :followings` before `has_many :celebrities, :through => :followings`: it's also more readable. – Max Williams Mar 30 '15 at 14:53
  • The one thing I find confusing is why Michael Hartl's tutorial for achieving such functionality is so much more complex. Isn't your example and his aiming to accomplish the same thing? https://www.railstutorial.org/book/following_users – Carl Edwards Mar 30 '15 at 14:57
  • Thanks @MaxWilliams I updated the answer. Hmm I'm not able to see the tutorial in your link, it's an empty white page for me. – Mike S Mar 30 '15 at 15:02
  • Strange. Try this link: https://www.railstutorial.org/book. I'm referring to Chapter 12 of the book. Let me know if that works. – Carl Edwards Mar 30 '15 at 15:05
  • I guess it's the machine I'm on. All I see is a white page with some UI elements but they don't work when I click them. – Mike S Mar 30 '15 at 15:07
  • @MaxWilliams judging by what you see and your experience, would I still be able to achieve what's shown towards the beginning of the chapter's tutorial? If so what is he doing that's different from Mike's example? – Carl Edwards Mar 30 '15 at 15:09
  • 2
    I think Mike Hartl's solution is basically the same as @MikeSlutsky's. He (MikeH) adds extra stuff like validation, and his association names are different, but it's basically still a join table to map a one-directional relationship. Obviously in his example the both ends of the relationship are the same type of object (User), instead of two (User, Celebrity) like in your case. Also he writes tests :) – Max Williams Mar 30 '15 at 15:16
  • Great! Thank you both for your help in getting me started. I searched for days on end trying to find a solution and never would've imagined it to be as simple as this. It might've been how I was interpreting the book's explanation but I was expecting the setup to be a lot harder. – Carl Edwards Mar 30 '15 at 15:19
  • That's the beauty of Rails @CarlEdwards :) – Mike S Mar 30 '15 at 15:20