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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:14:15+00:00 2026-05-13T06:14:15+00:00

consider this code class User < ActiveRecord::Base has_many :views has_many :posts, :through => :views,

  • 0

consider this code

class User < ActiveRecord::Base
  has_many :views
  has_many :posts, :through => :views, :uniq => true

  has_many :favorites
  has_many :posts, :through => :favorites, :uniq => true

  has_many :votes
  has_many :posts, :through => :votes, :uniq => true
end

# controller code

user = User.find(3)
posts = user.posts # ??

that said i have established three relationships between posts and users, through different way. But what about the last line??? how can I tell rails that I want to get the posts through views or favorites.

  • 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-13T06:14:16+00:00Added an answer on May 13, 2026 at 6:14 am

    You can give each association a different name, but point it at the same model using the :class_name option. Like so:

    class User < ActiveRecord::Base
      has_many :views
      has_many :view_posts, :through => :views, :class_name => 'Post', :uniq => true, 
    
      has_many :favorites
      has_many :favorite_posts, :through => :favorites, :class_name => 'Post', :uniq => true
    
      has_many :votes
      has_many :vote_posts, :through => :votes, :class_name => 'Post', :uniq => true
    end
    
    # Then...
    User.find(3).favorite_posts
    

    You may also find named_scope useful.

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

Sidebar

Related Questions

Yet another argument with a friend of mine. Consider this code: class User <
Consider this code: class DatabaseCommands { @Privilege(ADMIN) public void delete(Something thingToDelete, User currentUser) {
Consider this code : class MyClass<T> { } class AnotherClass : MyClass<String> { }
please consider this code : 1)public static class MyClass 2){ 3) public static DateTime
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Consider this code: package Prova; import java.util.ArrayList; public class Prova { private ArrayList<String> people;
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
Consider this code: if(initscr() == NULL) ERROR(Cannot start ncurses mode.\n); keypad(stdscr, TRUE); cbreak(); int
consider this code: class TestClass { function __construct() { set_error_handler(array($this,'error_handler')); } function error_handler($errno,$errstr,$errfile,$errline,$errcontext) {
Consider this code: <tr> <td> <input type=checkbox name=20081846 value=20081846 class=chk-input> </td> <td>20081846</td> <td>AGONOY, JAY

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.