cheat sheets.

$ cheat has_many
You use has_many in your ActiveRecord models.  In the class Order for your
orders table, you would write has_many :line_items to indicate the relationship
between Orders and Line Items.  In this case, you are saying an order has many
line items (and don't forget to say :belongs_to in the LineItem mode!!!)

Options for has_many

: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_many :products will
by default be linked to the Product class, but if the real class name is
SpecialProduct, you’ll have to specify it with this option.

:conditions - specify the conditions that the associated objects must meet in
order to be included as a "WHERE" sql fragment, such as "price > 5 AND name LIKE
‘B%’".

:order - specify the order in which the associated objects are returned as a
"ORDER BY" sql fragment, such as "last_name, first_name DESC"

:group - specify the attribute by which the associated objects are returned as a
"GROUP BY" sql fragment, such as "category"

: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_many association will use "person_id" as the
default foreign_key.

:dependent - if set to :destroy all the associated objects are destroyed
alongside this object by calling their destroy method. If set to :delete_all all
associated objects are deleted without calling their destroy method. If set to
:nullify all associated objects’ foreign keys are set to NULL without calling
their save callbacks. NOTE: :dependent => true is deprecated and has been
replaced with :dependent => :destroy. May not be set if :exclusively_dependent
is also set.

:exclusively_dependent - Deprecated; equivalent to :dependent => :delete_all. If
set to true all the associated object are deleted in one SQL statement without
having their before_destroy callback run. This should only be used on
associations that depend solely on this class and don’t need to do any
clean-up in before_destroy. The upside is that it’s much faster, especially if
there’s a counter_cache involved. May not be set if :dependent is also set.

:finder_sql - specify a complete SQL statement to fetch the association. This is
a good way to go for complex associations that depend on multiple tables. Note:
When this option is used, find_in_collection is not added.

:counter_sql - specify a complete SQL statement to fetch the size of the
association. If +:finder_sql+ is specified but +:counter_sql+, +:counter_sql+
will be generated by replacing SELECT … FROM with SELECT COUNT(*) FROM.

:extend - specify a named module for extending the proxy, see "Association
extensions".

:include - specify second-order associations that should be eager loaded when
the collection is loaded.

:group: An attribute name by which the result should be grouped. Uses the GROUP
BY SQL-clause.

:limit: An integer determining the limit on the number of rows that should be
returned.

:offset: An integer determining the offset from where the rows should be
fetched. So at 5, it would skip the first 4 rows.

:select: By default, this is * as in SELECT * FROM, but can be changed if you
for example want to do a join, but not include the joined columns.

:as: Specifies a polymorphic interface (See belongs_to).

:through: Specifies a Join Model to perform the query through. Options for
:class_name and :foreign_key are ignored, as the association uses the source
reflection. You can only use a :through query through a belongs_to or has_many
association.

:source: Specifies the source association name used by has_many :through
queries. Only use it if the name cannot be inferred from the association.
has_many :subscribers, :through => :subscriptions will look for either
+:subscribers+ or +:subscriber+ on Subscription, unless a +:source+ is given.
Version 2, updated 659 days ago.
. o 0 ( edit | previous | history | diff )
( add new | see all )