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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:38:26+00:00 2026-05-25T11:38:26+00:00

Newbie question, beware! I’d like to implement a basic many-to-many relationship in Rails and

  • 0

Newbie question, beware! I’d like to implement a basic many-to-many relationship in Rails and I’m trying to find out which approach is considered the most “rails way” of them. In a traditional non-ActiveRecord DB I’d just created two tables and a junction table and written a bunch of logic to make sure all three tables are taken in consideration when operations are performed on any of them.

This is my first time using an ORM and so I’m trying to find out if perhaps ActiveRecord somehow simplifies the process for you, perhaps by not requiring a junction table to be manually created.

Railscasts seems like a reputable source of Rails wisdom, are the two ways in this cast truly “Rails way” or can I do better? – http://railscasts.com/episodes/47-two-many-to-many

  • 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-25T11:38:26+00:00Added an answer on May 25, 2026 at 11:38 am

    There’s basically two ways: has_and_belongs_to_many (habtm) and has_many with a :through option that points to another association. Both require join tables; the latter is what we call a join model, because you typically add more information to the join.

    For example, consider an application with a User model who bookmarks Sites. One way would be to implement it as a habtm relationship

    class User < ActiveRecord::Base
      has_and_belongs_to_many :sites
    end
    class Site < ActiveRecord::Base
      has_and_belongs_to_many :users
    end
    
    user.sites << Site.find(...)
    

    This modeling will also require creating the sites_users table, which necessarily will lack a primary key.

    The problem with this is you’re likely to want to store additional information on it, so you might as well go with a join model, in this case Bookmark:

    class User < ActiveRecord::Base
      has_many :bookmarks
      has_many :sites, :through => :bookmarks
    end
    class Site < ActiveRecord::Base
      has_many :bookmarks
      has_many :users, :through => :bookmarks
      #edit: adding validation for requiring at least one bookmark
      validate_before_create :at_least_one_bookmark
      private
      def at_least_one_bookmark
        errors.add_to_base("requires at least one bookmark") unless bookmarks.count > 0
      end
    end
    class Bookmark < ActiveRecord::Base
      belongs_to :user
      belongs_to :site
    end
    
    user.bookmarks.create(:site => Site.find(...) )
    

    The more common pattern is the join model approach for its versatility and better modelling, though habtms are still used somewhat. They’re just so two-dimensional that you really need to examine what you’re doing and make sure there isn’t some richer behavior that needs to be modelled as well.

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

Sidebar

Related Questions

Newbie question, you've been warned! I'm trying to implement a sample Rails app with
Newbie question here. I'm in the beginning stages of laying out a site in
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie question since I'm not up to speed using maven at all. I'm trying
Total newbie question but this is driving me mad! I'm trying this: myInt =
Newbie question on xslt. I've multiple xsl:if checks like <xsl:if test=node/node1> ...</xsl:if> ... <xsl:if
Newbie question: I have a loop which places a number of elements on a
Flex newbie question: can MXML be generated on the fly, like HTML is generated
Django newbie question.... I'm trying to write a search form and maintain the state
A newbie question on thrift if anyone else is in a situation trying to

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.