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 8755809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:55:43+00:00 2026-06-13T13:55:43+00:00

I want to be able to show users who have liked a user’s post.

  • 0

I want to be able to show users who have liked a user’s post. I am going to provide a simple amount of information so this issue won’t get overwhelming.

I have three classes involved: User, Share and Opinion. Their models are set up likewise:

class User
  has_many :opinions, foreign_key: "fan_id", dependent: :destroy
end

class Share
  has_many :opinions, foreign_key: "like_id", dependent: :destroy
  has_many :likes, through: :opinions
  has_many :agreeables, foreign_key: "fan_id", class_name: "Opinion", dependent: :destroy
  has_many :fans, through: :agreeables
end

class Opinion
  belongs_to :fan, class_name: "User"
  belongs_to :like, class_name: "Share"
end

In other words: like_id = share.id & fan_id = user.id

Then in my controller:

SharesController
  def show
    @share = Share.find(params[:id])
    @shares = @share.fans.paginate(page: params[:page])
  end

I click the link to share_path, it renders the page but with nothing on it. When I look at my SQL logs, I notice the problem:

It is not inner joining opinions correctly to have the rendered share (like_id) match up with the users who liked the share (fan_ids).

Let’s say the share.id was 11 and the current_user.id is 5. It’s query consists primarily of inner joining opinions to have the like_id AND fan_id = 11.

Any ideas on how I can fix this problem or an alternate method that is more practical?

Thanks for your help.

Edit

For any future readers, the solution was as followed:

class User
  has_many :fans, foreign_key: "fan_id", class_name: "Opinion", dependent: :destroy
end

class Share
  has_many :opinions, foreign_key: "like_id", dependent: :destroy
  has_many :likes, through: :opinions
  has_many :fans, through: :opinions
end

And the SharesController and Opinion model was left unchanged.

  • 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-06-13T13:55:44+00:00Added an answer on June 13, 2026 at 1:55 pm

    If you want to get all users who liked a Share, the following code should be enough :

    class User
      has_many :opinions, foreign_key: "fan_id", dependent: :destroy
      has_many :shares, through: :opinions
    end
    
    class Share
      has_many :opinions, foreign_key: "like_id", dependent: :destroy
      has_many :fans, through: :opinions # I'm not sure, but you might need to add class_name option here
    end
    
    class Opinion
      belongs_to :fan, class_name: "User"
      belongs_to :like, class_name: "Share"
    end
    

    In your controller,
    @shares = @share.fans.paginate(page: params[:page]) will return the list of users(fans)

    EDIT : As you’re not using conventional names (fan instead of User and like instead of Share), you might need to add more options like inverse_of and class_name, but I’m not really sure about that

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

Sidebar

Related Questions

I want to be able to show some account information in a ring chart.
I want to be able to send updates (in news activity) to users who
I have an index page that I want to show all the users' profile
I want to be able to show the elapsed time in a textview or
Depending on where I use my Class, I want to be able to show
I want my app (AIR app ported on iOS) to be able to show
Want to be able to provide a search interface for a collection of objects
I want to be able to take an image that i have already captured
I want to have a table that holds information about a task. PriorityId, StateId,
I have a set of screens in C#/ASP where the user enters information. It's

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.