Module: BelongsToMany
- Extended by:
- Migrations
- Included in:
- ActiveRecord::Base
- Defined in:
- lib/vex/active_record/belongs_to_many.rb
Overview
belongs_to_many gives you a behaviour that you would typically (and for a reason) do via has_and_belongs_to_many or has_many. You would do this for performance reasons under a quite small set of circumstances:
-
your habtm code runs slow, because of the huge number of database transactions involved
-
you will never search by the associated data
In these cases belongs_to_many might help. It not only denormalizes the database structure, it uses a single text column in the host table to hold the referenced ids, and uses ActiveRecord’s read_attribute and write_attribute methods to automatically convert between IDs involved and actual objects.
belongs_to_many has a number of limitations over AR’ has_and_belongs_to_many and has_many associations:
-
it is not an association proxy: that means you cannot extend it, cannot search through it, etc.
-
The ‘<<’ operator is not allowed and throws an exceptions: As we wanted to stay pretty close to rails default behaviour we had to disable that method. Otherwise all performance gain would be lost anyways.
-
You cannot search via the associated data, because there is no easy way to join the associated tables.
-
There might be a bogus write when setting the column
ONCE MORE! THIS IS NOT A MORE PERFORMANT replacement for has_and_belongs_to_many or has_many!
Defined Under Namespace
Modules: ClassMethods, Migrations
Class Method Summary collapse
Methods included from Migrations
Class Method Details
.included(base) ⇒ Object
32 33 34 |
# File 'lib/vex/active_record/belongs_to_many.rb', line 32 def self.included(base) base.extend ClassMethods end |