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

block_params: 
comment: 
- !ruby/struct:SM::Flow::P 
  body: Specifies a one-to-one association with another class. This method should only be used if this class contains the foreign key. If the other class contains the foreign key, then you should use <tt>has_one</tt> instead. See also ActiveRecord::Associations::ClassMethods's overview on when to use <tt>has_one</tt> and when to use <tt>belongs_to</tt>.
- !ruby/struct:SM::Flow::P 
  body: "Methods will be added for retrieval and query for a single associated object, for which this object holds an id:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: association(force_reload = false)
    body: Returns the associated object. <tt>nil</tt> is returned if none is found.
  - !ruby/struct:SM::Flow::LI 
    label: association=(associate)
    body: Assigns the associate object, extracts the primary key, and sets it as the foreign key.
  - !ruby/struct:SM::Flow::LI 
    label: association.nil?
    body: Returns <tt>true</tt> if there is no associated object.
  - !ruby/struct:SM::Flow::LI 
    label: build_association(attributes = {})
    body: Returns a new object of the associated type that has been instantiated with <tt>attributes</tt> and linked to this object through a foreign key, but has not yet been saved.
  - !ruby/struct:SM::Flow::LI 
    label: create_association(attributes = {})
    body: Returns a new object of the associated type that has been instantiated with <tt>attributes</tt>, linked to this object through a foreign key, and that has already been saved (if it passed the validation).
  type: :LABELED
- !ruby/struct:SM::Flow::P 
  body: (<tt>association</tt> is replaced with the symbol passed as the first argument, so <tt>belongs_to :author</tt> would add among others <tt>author.nil?</tt>.)
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Example
- !ruby/struct:SM::Flow::P 
  body: "A Post class declares <tt>belongs_to :author</tt>, which will add:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Post#author</tt> (similar to <tt>Author.find(author_id)</tt>)
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Post#author=(author)</tt> (similar to <tt>post.author_id = author.id</tt>)
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Post#author?</tt> (similar to <tt>post.author == some_author</tt>)
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Post#author.nil?</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Post#build_author</tt> (similar to <tt>post.author = Author.new</tt>)
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Post#create_author</tt> (similar to <tt>post.author = Author.new; post.author.save; post.author</tt>)
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: The declaration can also 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_one :author</tt> will by default be linked to the Author class, but if the real class name is Person, you'll have to specify it with this option.
  - !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>.
  - !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: ":foreign_key"
    body: Specify the foreign key used for the association. By default this is guessed to be the name of the association with an &quot;_id&quot; suffix. So a class that defines a <tt>belongs_to :person</tt> association will use &quot;person_id&quot; as the default <tt>:foreign_key</tt>. Similarly, <tt>belongs_to :favorite_person, :class_name =&gt; &quot;Person&quot;</tt> will use a foreign key of &quot;favorite_person_id&quot;.
  - !ruby/struct:SM::Flow::LI 
    label: ":dependent"
    body: If set to <tt>:destroy</tt>, the associated object is destroyed when this object is. If set to <tt>:delete</tt>, the associated object is deleted <b>without</b> calling its destroy method. This option should not be specified when <tt>belongs_to</tt> is used in conjunction with a <tt>has_many</tt> relationship on another class because of the potential to leave orphaned records behind.
  - !ruby/struct:SM::Flow::LI 
    label: ":counter_cache"
    body: "Caches the number of belonging objects on the associate class through the use of <tt>increment_counter</tt> and <tt>decrement_counter</tt>. The counter cache is incremented when an object of this class is created and decremented when it's destroyed. This requires that a column named <tt>#{table_name}_count</tt> (such as <tt>comments_count</tt> for a belonging Comment class) is used on the associate class (such as a Post class). You can also specify a custom counter cache column by providing a column name instead of a <tt>true</tt>/<tt>false</tt> value to this option (e.g., <tt>:counter_cache =&gt; :my_custom_counter</tt>.) Note: Specifying a counter cache will add it to that model's list of readonly attributes using <tt>attr_readonly</tt>."
  - !ruby/struct:SM::Flow::LI 
    label: ":include"
    body: Specify second-order associations that should be eager loaded when this object is loaded.
  - !ruby/struct:SM::Flow::LI 
    label: ":polymorphic"
    body: "Specify this association is a polymorphic association by passing <tt>true</tt>. Note: If you've enabled the counter cache, then you may want to add the counter cache attribute to the <tt>attr_readonly</tt> list in the associated classes (e.g. <tt>class Post; attr_readonly :comments_count; end</tt>)."
  - !ruby/struct:SM::Flow::LI 
    label: ":readonly"
    body: If true, the associated object is 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>false</tt> by default.
  type: :LABELED
- !ruby/struct:SM::Flow::P 
  body: "Option examples:"
- !ruby/struct:SM::Flow::VERB 
  body: "  belongs_to :firm, :foreign_key =&gt; &quot;client_of&quot;\n  belongs_to :author, :class_name =&gt; &quot;Person&quot;, :foreign_key =&gt; &quot;author_id&quot;\n  belongs_to :valid_coupon, :class_name =&gt; &quot;Coupon&quot;, :foreign_key =&gt; &quot;coupon_id&quot;,\n             :conditions =&gt; 'discounts &gt; #{payments_count}'\n  belongs_to :attachable, :polymorphic =&gt; true\n  belongs_to :project, :readonly =&gt; true\n  belongs_to :post, :counter_cache =&gt; true\n"
full_name: ActiveRecord::Associations::ClassMethods#belongs_to
is_singleton: false
name: belongs_to
params: (association_id, options = {})
visibility: public
