--- !ruby/object:RI::MethodDescription 
aliases: []

block_params: 
comment: 
- !ruby/struct:SM::Flow::P 
  body: 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 &quot;developers_projects&quot; because &quot;D&quot; outranks &quot;P&quot;. Note that this precedence is calculated using the <tt>&lt;</tt> 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 &quot;paper_boxes&quot; and &quot;papers&quot; to generate a join table name of &quot;papers_paper_boxes&quot; because of the length of the name &quot;paper_boxes&quot;, but it in fact generates a join table name of &quot;paper_boxes_papers&quot;. Be aware of this caveat, and use the custom <tt>:join_table</tt> option if you need to.
- !ruby/struct:SM::Flow::P 
  body: "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:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class CreateDevelopersProjectsJoinTable &lt; ActiveRecord::Migration\n    def self.up\n      create_table :developers_projects, :id =&gt; false do |t|\n        t.integer :developer_id\n        t.integer :project_id\n      end\n    end\n\n    def self.down\n      drop_table :developers_projects\n    end\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: "Deprecated: Any additional fields added to the join table will be placed as attributes when pulling records out through <tt>has_and_belongs_to_many</tt> associations. Records returned from join tables with additional attributes will be marked as readonly (because we can't save changes to the additional attributes). It's strongly recommended that you upgrade any associations with attributes to a real join model (see introduction)."
- !ruby/struct:SM::Flow::P 
  body: "Adds the following methods for retrieval and query:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: collection(force_reload = false)
    body: Returns an array of all the associated objects. An empty array is returned if none are found.
  - !ruby/struct:SM::Flow::LI 
    label: collection<<(object, ...)
    body: Adds one or more objects to the collection by creating associations in the join table (<tt>collection.push</tt> and <tt>collection.concat</tt> are aliases to this method).
  - !ruby/struct:SM::Flow::LI 
    label: collection.delete(object, ...)
    body: Removes one or more objects from the collection by removing their associations from the join table. This does not destroy the objects.
  - !ruby/struct:SM::Flow::LI 
    label: collection=objects
    body: Replaces the collection's content by deleting and adding objects as appropriate.
  - !ruby/struct:SM::Flow::LI 
    label: collection_singular_ids
    body: Returns an array of the associated objects' ids.
  - !ruby/struct:SM::Flow::LI 
    label: collection_singular_ids=ids
    body: Replace the collection by the objects identified by the primary keys in <tt>ids</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: collection.clear
    body: Removes every object from the collection. This does not destroy the objects.
  - !ruby/struct:SM::Flow::LI 
    label: collection.empty?
    body: Returns <tt>true</tt> if there are no associated objects.
  - !ruby/struct:SM::Flow::LI 
    label: collection.size
    body: Returns the number of associated objects.
  - !ruby/struct:SM::Flow::LI 
    label: collection.find(id)
    body: Finds an associated object responding to the <tt>id</tt> and that meets the condition that it has to be associated with this object. Uses the same rules as ActiveRecord::Base.find.
  - !ruby/struct:SM::Flow::LI 
    label: collection.exists?(...)
    body: Checks whether an associated object with the given conditions exists. Uses the same rules as ActiveRecord::Base.exists?.
  - !ruby/struct:SM::Flow::LI 
    label: collection.build(attributes = {})
    body: Returns a new object of the collection type that has been instantiated with <tt>attributes</tt> and linked to this object through the join table, but has not yet been saved.
  - !ruby/struct:SM::Flow::LI 
    label: collection.create(attributes = {})
    body: Returns a new object of the collection type that has been instantiated with <tt>attributes</tt>, linked to this object through the join table, and that has already been saved (if it passed the validation).
  type: :LABELED
- !ruby/struct:SM::Flow::P 
  body: (<tt>collection</tt> is replaced with the symbol passed as the first argument, so <tt>has_and_belongs_to_many :categories</tt> would add among others <tt>categories.empty?</tt>.)
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Example
- !ruby/struct:SM::Flow::P 
  body: "A Developer class declares <tt>has_and_belongs_to_many :projects</tt>, which will add:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects&lt;&lt;</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects.delete</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects=</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#project_ids</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#project_ids=</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects.clear</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects.empty?</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects.size</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects.find(id)</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#clients.exists?(...)</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects.build</tt> (similar to <tt>Project.new(&quot;project_id&quot; =&gt; id)</tt>)
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new(&quot;project_id&quot; =&gt; id); c.save; c</tt>)
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: The declaration may include an options hash to specialize the behavior of the association.
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Options
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: ":class_name"
    body: Specify the class name of the association. Use it only if that name can't be inferred from the association name. So <tt>has_and_belongs_to_many :projects</tt> 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.
  - !ruby/struct:SM::Flow::LI 
    label: ":join_table"
    body: Specify the name of the join table if the default based on lexical order isn't what you want. <b>WARNING:</b> If you're overwriting the table name of either class, the <tt>table_name</tt> method MUST be declared underneath any <tt>has_and_belongs_to_many</tt> declaration in order to work.
  - !ruby/struct:SM::Flow::LI 
    label: ":foreign_key"
    body: Specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and &quot;_id&quot; suffixed. So a Person class that makes a <tt>has_and_belongs_to_many</tt> association to Project will use &quot;person_id&quot; as the default <tt>:foreign_key</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: ":association_foreign_key"
    body: 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 &quot;_id&quot; suffixed. So if a Person class makes a <tt>has_and_belongs_to_many</tt> association to Project, the association will use &quot;project_id&quot; as the default <tt>:association_foreign_key</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: ":conditions"
    body: Specify the conditions that the associated object must meet in order to be included as a <tt>WHERE</tt> SQL fragment, such as <tt>authorized = 1</tt>. Record creations from the association are scoped if a hash is used. <tt>has_many :posts, :conditions =&gt; {:published =&gt; true}</tt> will create published posts with <tt>@blog.posts.create</tt> or <tt>@blog.posts.build</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: ":order"
    body: Specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment, such as <tt>last_name, first_name DESC</tt>
  - !ruby/struct:SM::Flow::LI 
    label: ":uniq"
    body: If true, duplicate associated objects will be ignored by accessors and query methods.
  - !ruby/struct:SM::Flow::LI 
    label: ":finder_sql"
    body: Overwrite the default generated SQL statement used to fetch the association with a manual statement
  - !ruby/struct:SM::Flow::LI 
    label: ":counter_sql"
    body: Specify a complete SQL statement to fetch the size of the association. If <tt>:finder_sql</tt> is specified but not <tt>:counter_sql</tt>, <tt>:counter_sql</tt> will be generated by replacing <tt>SELECT ... FROM</tt> with <tt>SELECT COUNT(*) FROM</tt>.
  - !ruby/struct:SM::Flow::LI 
    label: ":delete_sql"
    body: Overwrite the default generated SQL statement used to remove links between the associated classes with a manual statement.
  - !ruby/struct:SM::Flow::LI 
    label: ":insert_sql"
    body: Overwrite the default generated SQL statement used to add links between the associated classes with a manual statement.
  - !ruby/struct:SM::Flow::LI 
    label: ":extend"
    body: Anonymous module for extending the proxy, see &quot;Association extensions&quot;.
  - !ruby/struct:SM::Flow::LI 
    label: ":include"
    body: Specify second-order associations that should be eager loaded when the collection is loaded.
  - !ruby/struct:SM::Flow::LI 
    label: ":group"
    body: An attribute name by which the result should be grouped. Uses the <tt>GROUP BY</tt> SQL-clause.
  - !ruby/struct:SM::Flow::LI 
    label: ":having"
    body: Combined with +:group+ this can be used to filter the records that a <tt>GROUP BY</tt> returns. Uses the <tt>HAVING</tt> SQL-clause.
  - !ruby/struct:SM::Flow::LI 
    label: ":limit"
    body: An integer determining the limit on the number of rows that should be returned.
  - !ruby/struct:SM::Flow::LI 
    label: ":offset"
    body: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
  - !ruby/struct:SM::Flow::LI 
    label: ":select"
    body: By default, this is <tt>*</tt> as in <tt>SELECT * FROM</tt>, but can be changed if, for example, you want to do a join but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will raise an error.
  - !ruby/struct:SM::Flow::LI 
    label: ":readonly"
    body: If true, all the associated objects are readonly through the association.
  - !ruby/struct:SM::Flow::LI 
    label: ":validate"
    body: If false, don't validate the associated objects when saving the parent object. <tt>true</tt> by default.
  - !ruby/struct:SM::Flow::LI 
    label: ":autosave"
    body: If true, always save any loaded members and destroy members marked for destruction, when saving the parent object. Off by default.
  type: :LABELED
- !ruby/struct:SM::Flow::P 
  body: "Option examples:"
- !ruby/struct:SM::Flow::VERB 
  body: "  has_and_belongs_to_many :projects\n  has_and_belongs_to_many :projects, :include =&gt; [ :milestones, :manager ]\n  has_and_belongs_to_many :nations, :class_name =&gt; &quot;Country&quot;\n  has_and_belongs_to_many :categories, :join_table =&gt; &quot;prods_cats&quot;\n  has_and_belongs_to_many :categories, :readonly =&gt; true\n  has_and_belongs_to_many :active_projects, :join_table =&gt; 'developers_projects', :delete_sql =&gt;\n  'DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}'\n"
full_name: ActiveRecord::Associations::ClassMethods#has_and_belongs_to_many
is_singleton: false
name: has_and_belongs_to_many
params: (association_id, options = {}, &extension)
visibility: public
