Skip to content

Has and belongs to many association

Andrew Buntine edited this page Jan 23, 2014 · 8 revisions

Synopsys:

For a multiselect widget: (natural choice for n-n associations)

class Team < ActiveRecord::Base
  has_and_belongs_to_many :fans

  attr_accessible :fan_ids

Or for a nested form:

class Team < ActiveRecord::Base
  has_and_belongs_to_many :fans

  accepts_nested_attributes_for :fans, :allow_destroy => true
  attr_accessible :fans_attributes

  rails_admin do
    configure :fans do
      inverse_of :teams
      # configuration here
    end
  end
end

# for info
class Fan < ActiveRecord::Base
  has_and_belongs_to_many :teams
end

More here

Clone this wiki locally