Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (40 sloc) 4.23 KB
---
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!!!)\r\n\
\r\n\
Options for has_many\r\n\
\r\n\
:class_name - specify the class name of the association. Use it only if that name can\xE2\x80\x99t 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\xE2\x80\x99ll have to specify it with this option.\r\n\
\r\n\
: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 \xE2\x80\x98B%\xE2\x80\x99\".\r\n\
\r\n\
:order - specify the order in which the associated objects are returned as a \"ORDER BY\" sql fragment, such as \"last_name, first_name DESC\"\r\n\
\r\n\
:group - specify the attribute by which the associated objects are returned as a \"GROUP BY\" sql fragment, such as \"category\"\r\n\
\r\n\
: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.\r\n\
\r\n\
: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\xE2\x80\x99 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.\r\n\
\r\n\
: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\xE2\x80\x99t need to do any clean-up in before_destroy. The upside is that it\xE2\x80\x99s much faster, especially if there\xE2\x80\x99s a counter_cache involved. May not be set if :dependent is also set.\r\n\
\r\n\
: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.\r\n\
\r\n\
: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 \xE2\x80\xA6 FROM with SELECT COUNT(*) FROM.\r\n\
\r\n\
:extend - specify a named module for extending the proxy, see \"Association extensions\".\r\n\
\r\n\
:include - specify second-order associations that should be eager loaded when the collection is loaded.\r\n\
\r\n\
:group: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.\r\n\
\r\n\
:limit: An integer determining the limit on the number of rows that should be returned.\r\n\
\r\n\
:offset: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.\r\n\
\r\n\
: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.\r\n\
\r\n\
:as: Specifies a polymorphic interface (See belongs_to).\r\n\
\r\n\
: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.\r\n\
\r\n\
: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."