Specifies a many-to-many relationship with another class. This associates
two classes via an intermediate join table. Unless the join table is
explicitly specified as an option, it is guessed using the lexical order of
the class names. So a join between Developer and Project will give the default join table name
of âdevelopers_projectsâ because âDâ precedes âPâ alphabetically. Note that
this precedence is calculated using the <
operator for String. This means that if the strings are of
different lengths, and the strings are equal when compared up to the
shortest length, then the longer string is considered of higher lexical
precedence than the shorter one. For example, one would expect the tables
âpaper_boxesâ and âpapersâ to generate a join table name of
âpapers_paper_boxesâ because of the length of the name âpaper_boxesâ, but
it in fact generates a join table name of âpaper_boxes_papersâ. Be aware of
this caveat, and use the custom :join_table
option if you need
to. If your tables share a common prefix, it will only appear once at the
beginning. For example, the tables âcatalog_categoriesâ and
âcatalog_productsâ generate a join table name of
âcatalog_categories_productsâ.
The join table should not have a primary key or a model associated with it. You must manually generate the join table with a migration such as this:
class CreateDevelopersProjectsJoinTable < ActiveRecord::Migration def change create_table :developers_projects, id: false do |t| t.integer :developer_id t.integer :project_id end end end
It's also a good idea to add indexes to each of those columns to speed up the joins process. However, in MySQL it is advised to add a compound index for both of the columns as MySQL only uses one index per table during the lookup.
Adds the following methods for retrieval and query:
- collection(force_reload = false)
-
Returns an array of all the associated objects. An empty array is returned if none are found.
- collection<<(object, â¦)
-
Adds one or more objects to the collection by creating associations in the join table (
collection.push
andcollection.concat
are aliases to this method). Note that this operation instantly fires update SQL without waiting for the save or update call on the parent object, unless the parent object is a new record. - collection.delete(object, â¦)
-
Removes one or more objects from the collection by removing their associations from the join table. This does not destroy the objects.
- collection.destroy(object, â¦)
-
Removes one or more objects from the collection by running destroy on each association in the join table, overriding any dependent option. This does not destroy the objects.
- collection=objects
-
Replaces the collection's content by deleting and adding objects as appropriate.
- collection_singular_ids
-
Returns an array of the associated objects' ids.
- collection_singular_ids=ids
-
Replace the collection by the objects identified by the primary keys in
ids
. - collection.clear
-
Removes every object from the collection. This does not destroy the objects.
- collection.empty?
-
Returns
true
if there are no associated objects. - collection.size
-
Returns the number of associated objects.
- collection.find(id)
-
Finds an associated object responding to the
id
and that meets the condition that it has to be associated with this object. Uses the same rules asActiveRecord::Base.find
. - collection.exists?(â¦)
-
Checks whether an associated object with the given conditions exists. Uses the same rules as
ActiveRecord::Base.exists?
. - collection.build(attributes = {})
-
Returns a new object of the collection type that has been instantiated with
attributes
and linked to this object through the join table, but has not yet been saved. - collection.create(attributes = {})
-
Returns a new object of the collection type that has been instantiated with
attributes
, linked to this object through the join table, and that has already been saved (if it passed the validation).
(collection
is replaced with the symbol passed as the first
argument, so has_and_belongs_to_many :categories
would add
among others categories.empty?
.)
Example
A Developer
class declares has_and_belongs_to_many :projects
, which will
add:
-
Developer#projects
-
Developer#projects<<
-
Developer#projects.delete
-
Developer#projects.destroy
-
Developer#projects=
-
Developer#project_ids
-
Developer#project_ids=
-
Developer#projects.clear
-
Developer#projects.empty?
-
Developer#projects.size
-
Developer#projects.find(id)
-
Developer#projects.exists?(...)
-
Developer#projects.build
(similar toProject.new("developer_id" => id)
) -
Developer#projects.create
(similar toc = Project.new("developer_id" => id); c.save; c
)
The declaration may include an options hash to specialize the behavior of the association.
Options
- :class_name
-
Specify the class name of the association. Use it only if that name can't be inferred from the association name. So
has_and_belongs_to_many :projects
will by default be linked to the Project class, but if the real class name is SuperProject, you'll have to specify it with this option. - :join_table
-
Specify the name of the join table if the default based on lexical order isn't what you want. WARNING: If you're overwriting the table name of either class, the
table_name
method MUST be declared underneath anyhas_and_belongs_to_many
declaration in order to work. - :foreign_key
-
Specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and â_idâ suffixed. So a Person class that makes a
has_and_belongs_to_many
association to Project will use âperson_idâ as the default:foreign_key
. - :association_foreign_key
-
Specify the foreign key used for the association on the receiving side of the association. By default this is guessed to be the name of the associated class in lower-case and â_idâ suffixed. So if a Person class makes a
has_and_belongs_to_many
association to Project, the association will use âproject_idâ as the default:association_foreign_key
. - :readonly
-
If true, all the associated objects are readonly through the association.
- :validate
-
If
false
, don't validate the associated objects when saving the parent object.true
by default. - :autosave
-
If true, always save the associated objects or destroy them if marked for destruction, when saving the parent object. If false, never save or destroy the associated objects. By default, only save associated objects that are new records.
Note that
accepts_nested_attributes_for
sets:autosave
totrue
.
Option examples:
has_and_belongs_to_many :projects has_and_belongs_to_many :projects, -> { includes :milestones, :manager } has_and_belongs_to_many :nations, class_name: "Country" has_and_belongs_to_many :categories, join_table: "prods_cats" has_and_belongs_to_many :categories, -> { readonly }
Please login to continue.