Ask A Question

Notifications

You’re not receiving notifications from this thread.

Best Gem and Instructions for setting up followers one way.

Gerard Donnelly asked in General

Hi Guys,
I have an artist model and I want users to be able to follow them without being followed back. Is there a gem that I can use to do this and is there any documentation on setting that up?

Thanks in advance for any advice.

Gerard

Reply

Depending on how your database schema is set up, I'm not sure you'd need a gem to do it. I think, if you create a has_many, through: association between the artist model and the user model, that should be sufficient.

For example, if you create a new model called followers then you can add this line to the artist model:

class Artist < ApplicationRecord
    ...
    has_many :users, through: :followers
end

Similarly on the user model, you'd say:

class User < ApplicationRecord
    ...
    has_many :artists, through: :followers
end

Finally, on the followers model, all you say is:

class Follower < ApplicationRecord
    belongs_to :artist
    belongs_to :user
end

In this way, you can allow a user to 'follow' many artists and allow an artist to have many 'followers' - but unless the artist chooses to also follow a user, it will not happen. (If you want to dis-allow an artist from being able to follow a user, then that can probably be done by removing the 'follow' functionality for artists only within the views or something)

Reply

Thanks for this explanation Umar, I will test it out and let you know how I get on.

Thanks,

Gerard

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.