What is the fastest way to create mass HABTM associations in Rails?
问题 I have two tables, with a HABTM relationship in Rails. Something like the following: class Foo < ActiveRecord::Base has_and_belongs_to_many :bars end class Bar < ActiveRecord::Base has_and_belongs_to_many :foos end Now I have a new Foo object, and want to mass-assign thousands of bars to it, which I've pre-loaded: @foo = Foo.create @bars = Bar.find_all_by_some_attribute(:a) What's the fastest way to do this? I've tried: @foo.bars = @bars @foo.bars << @bars And both run really slow,