Rails HABTM Scoping to current subdomain

Posted by Rabbott on Stack Overflow See other posts from Stack Overflow or by Rabbott
Published on 2010-04-21T14:32:54Z Indexed on 2010/04/21 14:53 UTC
Read the original article Hit count: 297

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about habtm