Options for belongs_to
: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_one :author 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.
:conditions - specify the conditions that the associated object must meet in
order to be included as a "WHERE" sql fragment, such as "authorized = 1".
:order - specify the order from which the associated object will be picked at
the top. Specified as an "ORDER BY" sql fragment, such as "last_name, first_name
DESC"
:foreign_key - specify the foreign key used for the association. By default this
is guessed to be the name of the associated class in lower-case and "_id"
suffixed. So a Person class that makes a belongs_to association to a Boss class
will use "boss_id" as the default foreign_key.
:counter_cache - caches the number of belonging objects on the associate class
through use of increment_counter and decrement_counter. 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 "#{table_name}_count" (such as
comments_count 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
given that name instead of a true/false value to this option (e.g.,
:counter_cache => :my_custom_counter.)
:include - specify second-order associations that should be eager loaded when
this object is loaded.
:polymorphic - specify this association is a polymorphic association by passing
true.Version
1, updated 690 days ago.
. o 0 (
edit |
history )