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

  • Home
  • SEARCH
  • 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 7714857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:14:17+00:00 2026-06-01T02:14:17+00:00

I have two model: User, Article A user can like or dislike many articles,

  • 0

I have two model: User, Article

A user can like or dislike many articles, and an article can be liked or disliked by many users. So I need to build a many-to-many relation between them.

In rails, I think I need to use has_and_belongs_to_many..

I think the structure of the code is like below:

class User
    has_and_belongs_to_many :liked_articles
    has_and_belongs_to_many :disliked_articles
end

class Article
    has_and_belongs_to_many :liking_users
    has_and_belongs_to_many :disliking_users
end

Of course, the code above doesn’t work.

But I don’t know what the right code is. Who can help me?

Updated:

I came up with such code:

class User
    has_and_belongs_to_many :liked_articles, :class_name => 'Article', :join_table => 'articles_users_like', :uniq => true
    has_and_belongs_to_many :disliked_articles, :class_name => 'Article', :join_table => 'articles_users_dislike', :uniq => true
end

class Article
    has_and_belongs_to_many :liking_users, :class_name => 'User', :join_table => 'articles_users_like', :uniq => true
    has_and_belongs_to_many :disliking_users, :class_name => 'User', :join_table => 'articles_users_dislike', :uniq => true
end

I think it will work.

but as both Peter Sobot and Jon McIntosh said, nowadays has_many, :through is more preferred. Can you tell me why?
The latter one is logically more clear than has_and_belongs_to?

  • 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-01T02:14:19+00:00Added an answer on June 1, 2026 at 2:14 am

    These association methods are tied to Models. Unless you have a Liking_User and a Disliking_User model, this clearly won’t work. Apart from that I believe that you’re going at this the wrong way.

    Let’s break this problem down for a moment. You have a User and an Article. Your Article needs to have functionality to be disliked and liked by Users, correct? I’m going to visualize it sort-of like the SO/Reddit reputation system and assume that the User can like or dislike an Article once. For this I would create a separate model for voting, called Votes and tie it all together with a has_many, :through association.

    User.rb

      class User < ActiveRecord::Base
         has_many :votes
         has_many :articles, :through => :votes
       end
    

    Article.rb

      class Article < ActiveRecord::Base
         has_many :votes
         has_many :users, :through => :votes
      end
    

    Vote.rb

      class Vote < ActiveRecord::Base
         belongs_to :user
         belongs_to :article
      end
    

    Now that these associations are set up, the models are going to be looking for a few fields in the database. The structure should look something like this:

    Users
    +----+-----------+
    | id | user_name | 
    +----+-----------+    
    
    Articles
    +----+--------+
    | id |  title |
    +----+--------+
    
    Votes
    +----+----------+------------+-------+
    | id |  user_id | article_id | value |
    +----+----------+------------+-------+
    

    This completes the association by giving the model an ID to reference. Now you can witness the rails magic when you create that vote entry…

    class ArticlesController < ApplicationController
      def like
        @article = Article.find(params[:article_id])
        @article.votes.create(:user_id => current_user, :value => 1)
    
        respond_to do |format|
          if @article.save
            format.html { redirect_to @article, notice: 'Article was successfully liked.' }
            format.json { head :no_content }
          else
            format.html { redirect_to @article, notice: 'Article was unsuccessfully liked.' }
            format.json { render json: @article.errors, status: :unprocessable_entity }
          end
        end
    
      end
    end
    

    EDIT: Per your comment, the has_and_belongs_to_many and has_many :through relationships are very similar… You have two models and a join table to hold the ref data. The difference between the two is that has_many :through requires a third model which is interacted with between the relationship (like say, users/articles/votes).

    The reason why has_many :through is more widely recommended is that it’s more flexible and you’re given more to work with as opposed to the latter. In your case you must create this relationship because you need the third model to tally your votes.

    The logic that you’re following right now institutes a principal to create four Models, and would require updating specific ones based upon the action that the user wants to take. This is absolutely not the rails (or any logical) way of doing things. Keep it simple.

    Ruby on Rails Guides: A Guide to Active Record Associations

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

Sidebar

Related Questions

I have two models, Users and Groups. Each group can have many users and
i have two model 'Asset' , and 'User' .'Asset' can be assigned to one
Suppose you have two models, User and City, joined by a third model CityPermission:
I have a two models set up like this: class User < ActiveRecord::Base #
I have two tables related: DataEntered and Model DataEntered -currentModel Model One DataEntered can
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have two models: User MentoringRelationship MentoringRelationship is a join model that has a
I have two models: User and Base. User model: http://pastebin.com/WdLzBkHJ Base model: http://pastebin.com/tQrEUaSu At
I have two model's Contact, and User. When I create a new user I
I have two models, User and Discussion User model has_and_belongs_to_many :subscribed_discussions, :class_name => 'Discussion',

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.