All threads / Best Gem and Instructions for setting up followers one way.

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

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)

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

Thanks,

Gerard

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.