2009年4月20日星期一

Ruby on Rails ActiveRecord中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%’. Record creations from the association are scoped if a hash is used. has_many :posts, :conditions => {:published => true} will create published posts with @blog.posts.create or @blog.posts.build.
:order
Specify the order in which the associated objects are returned 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 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.
:primary_key
此项用来设置关联主键,默认为ID。
:dependent
如果此项设置为:destroy那么当删除此对象时,会关联删除many端的所有对象,并且会在删除之前调用destroy方法。如果设置为:delete_all这同样是关联删除,但是不会调用destroy方法。如果设置为:nullify,则会将所有的外键设置为null而不删除。
*注意:* 当启用 :through 选项时,:dependent选项将被忽略.
: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 not :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.
:having
Combined with +:group+ this can be used to filter the records that a GROUP BY returns. Uses the HAVING 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. Do not forget to include the primary and foreign keys, otherwise it will raise an error.
:as
Specifies a polymorphic interface (See belongs_to).
:through
Specifies a Join Model through which to perform the query. 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 on the join model.
: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.
:source_type
Specifies type of the source association used by has_many :through queries where the source association is a polymorphic belongs_to.
:uniq
If true, duplicates will be omitted from the collection. Useful in conjunction with :through.
:readonly
If true, all the associated objects are readonly through the association.
:validate
If false, don‘t validate the associated objects when saving the parent object. true by default.
:autosave
If true, always save any loaded members and destroy members marked for destruction, when saving the parent object. Off by default.

没有评论:

发表评论