Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6373597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:22:55+00:00 2026-05-25T01:22:55+00:00

On my project a User can follow some topics class User < ActiveRecord::Base has_many

  • 0

On my project a User can follow some topics

class User < ActiveRecord::Base
  has_many :topic_followings 
  has_many :topics, :through => :topic_followings, :include => [:questions]

Then I have a page where I’m displaying all the topics.
Example: Health [Follow], Science [Follow]

[Follow] is a link to a controller that will follow or unfollow the topic depending on whether the user is following the topic or not.

When I’m displaying all the topics I want the text of the link [Follow] to change depending on whether the user is following the topic or not.

Here is the code for the link

<%= link_to 'Follow',new_topic_following_path(:topic_id => topic.id) %> 

Is there any way to do this?
The only way I can think is use 1 SQL query for each topic but this is not very efficient.
Any practices that are better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:22:55+00:00Added an answer on May 25, 2026 at 1:22 am

    You could do a query for an array of all the topic ids that the user is following. So in your view, or a helper method, you could simply check if the array contains topic.id.

    # add this either into your controller, or separate the query into the model file as a separate method
    f_topics = TopicFollowing.where(:user_id => current_user.id).select(:topic_id)
    @followed_topics = f_topics.collect { |t| t = t.topic_id }
    
    <% @topics.each do |topic| %>
      <% if @followed_topics.include?(topic.id) %>
        # unfollow link here
      <% else %>
        follow link here
      <% end %>
    <% end %>
    

    Code is untested, but i think that should roughly work. Be sure to change the user references and columns as necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When showing a project, the user can add a decision via a form_for and
I want to implement user stories in a new project where can i find
So I'm trying to add some ability to my project to allow user-defined properties
Distributing ASP.NET user controls across different projects can be really useful when you want
I have to start a new project where user authentication/management will be required. A
My solution has a C# application project C# User Controls project C++ Mathematics project
I am working on a project where the user will transfer his username to
I'm working on a project which uses user authentication. I'm facing a issue with
Where would you place the SaveSettings method in your project when the user is
The Reporting Database in Project Server 2007 has about 7 User Views based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.