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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:29:21+00:00 2026-05-26T12:29:21+00:00

I have two models Site and User. When a user registers with the app

  • 0

I have two models Site and User. When a user registers with the app they choose a site to belong to. So a site has_many :users and a user belongs_to :site.

app\models\site.rb

class Site < ActiveRecord::Base

has_many :users

end

app\models\user.rb

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

    belongs_to :site
    has_and_belongs_to_many :roles, :uniq => true

end

This really reflects a user’s home site and I often call the relationship to find out where the user is from, for instance:

<%= user.lastname %>, <%= user.firstname %> from Site: <%= user.site.name %>
which translates to:
Smith, John from Site: GenericCo Operations

I would like to add a habtm relationship where users can SUPPORT many sites, so a user would be able to choose which sites they support from check boxes listing the sites.

I am familiar with the habtm relationship because my users have and belong to many roles. I established this as

has_and_belongs_to_many :roles, :uniq => true

I know for a fact that if I add a join table and use

has_and_belongs_to_many :sites

on my model everything will go crazy with tons of errors. I would appreciate any helpful advice and code hints.

  • 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-26T12:29:22+00:00Added an answer on May 26, 2026 at 12:29 pm

    The first argument to habtm is the name of the method you’ll call to get the collection. It can be anything you want, so long as you specify the model class with :class_name

    has_and_belongs_to_many :supported_sites, :class_name => 'Site'
    

    Depending on how you name things in your database, you might also need to specify :join_table, :foreign_key, or :association_foreign_key. Take a look at the Options section of http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many for more info.

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

Sidebar

Related Questions

I have two models: class Group < ActiveRecord::Base belongs_to :sites end class Site <
I have the following two models, User.. class User < ActiveRecord::Base has_and_belongs_to_many :sites end
I have two RoR3 application: http://users.domain.local http://profiles.domain.local I created the 'users/models/profile.rb': class Profile <
I have two models indexed for searching (User and Item). I'm trying to do
I have two models, user and group. I also have a joining table groups_users.
I am creating a site in Ruby on Rails, I have two models a
I have a rails site with two models: events and dates events has many
I have a Rails 2.3.11 app which has two key Models: Activity Transaction Live
My site has two types of users; customers and suppliers. I have ended up
I have two models, Article and Post that both inherit from a base model

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.