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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:51:30+00:00 2026-05-14T14:51:30+00:00

I have the following associations: class User < ActiveRecord::Base has_and_belongs_to_many :brands, :join_table => ‘brands_users’

  • 0

I have the following associations:

class User < ActiveRecord::Base
  has_and_belongs_to_many :brands, :join_table => 'brands_users'
  has_and_belongs_to_many :companies, :join_table => 'companies_users'
end

class Brand < ActiveRecord::Base
  belongs_to                :company
  has_and_belongs_to_many   :users, :join_table => 'brands_users'
end

class Company < ActiveRecord::Base
  has_and_belongs_to_many   :users, :join_table => 'companies_users'
  has_many :brands, :order => :name
end

While editing a user I am using the checkbox list of brands. So that I can assign users ‘access’ to brands, the brands that show up are only the brands that belong to the current company (defined by the subdomain [using subdomain_fu]).

The problem I am running into is that when using the default HABTM functionality and the checkbox list, upon save, Rails removes ALL user->brand associations, then re-adds just the ones for the form I just submitted..

How do I scope that to only remove associations of brands who belong to the current company, defined in the subdomain?

  • 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-14T14:51:30+00:00Added an answer on May 14, 2026 at 2:51 pm

    Here is what I did.. I ended up placing it in the controller, and manually adding ALL the outside values before saving the user.

    # if admin clears all brand checkboxes the browser will ignore this change,
    # so we will provide an empty array if this is the case, to make sure that
    # the brands are removed
    params[:user][:brand_ids] ||= []
    
    @user = User.find(params[:id])
    
    # collect brands for this user that are not part of this form to ensure they 
    # arent removed by the rails habtm functionality
    other_brands = @user.brands(:conditions => ['id NOT IN (?)', @company.brands])
    other_brands.each do |ob|
      params[:user][:brand_ids] << ob.id
    end
    
    # reload the user object with the brands selected on this form, as well as 
    # all their brands from other companies
    @user.reload(params)
    

    If anyone has a better idea, I would still love to hear it, as I don’t know if this is the best option here..

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

Sidebar

Related Questions

I have three Models setup with the following associations class User < ActiveRecord::Base has_many
I have the following models, with their relevant associations: class User < ActiveRecord::Base has_many
I have the following two models, User.. class User < ActiveRecord::Base has_and_belongs_to_many :sites end
I have the following associations class User < ActiveRecord::base has_many :memberships end What I
I have the following model associations: class Slider < ActiveRecord::Base has_one :featured_happening, :as =>
I currently have the following models: class Player < ActiveRecord::Base belongs_to :team belongs_to :user
I have following structure: class User < ActiveRecord::Base has_many :Hobbies, :dependent => :destroy accepts_nested_attributes_for
I have several classes associated as following: class User < ActiveRecord::Base has_many :posts, :dependent
I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
I have two models: User and Car with the following associations: User has_many Car

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.