Module: ActiveResource::Associations
- Included in:
- Base
- Defined in:
- lib/active_resource/associations.rb
Overview
Reproduced from https://github.com/rails/rails/pull/230/files for adding ActiveResource association support
Defined Under Namespace
Modules: Builder
Instance Method Summary collapse
-
#belongs_to(name, options = {}) ⇒ Object
Specifies a one-to-one association with another class.
-
#has_many(name, options = {}) ⇒ Object
Specifies a one-to-many association.
-
#has_one(name, options = {}) ⇒ Object
Specifies a one-to-one association.
Instance Method Details
#belongs_to(name, options = {}) ⇒ Object
Specifies a one-to-one association with another class. This class should only be used if this class contains the foreign key.
Methods will be added for retrieval and query for a single associated object, for which this object holds an id:
[association(force_reload = false)]
Returns the associated object. nil is returned if the foreign key is nil.
Throws a ActiveResource::ResourceNotFound exception if the foreign key is not nil
and the resource is not found.
(+association+ is replaced with the symbol passed as the first argument, so belongs_to :post would add among others post.nil?.
Example
A Comment class declaress belongs_to :post, which will add:
- Comment#post (similar to Post.find(post_id)) The declaration can also include an options hash to specialize the behavior of the association.
Options
[:class_name] Specify the class name for the association. Use it only if that name canÄt be inferred from association name. So belongs_to :post will by default be linked to the Post class, but if the real class name is Article, you'll have to specify it with whis option. [:foreign_key] Specify the foreign key used for the association. By default this is guessed to be the name of the association with an "_id" suffix. So a class that defines a belongs_to :post association will use "post_id" as the default :foreign_key. Similarly, belongs_to :article, :class_name => "Post" will use a foreign key of "article_id".
Option exampples: belongs_to :customer, :class_name => 'User' Creates a belongs_to association called customer which is represented through the User class.
belongs_to :customer, :foreign_key => 'user_id' Creates a belongs_to association called customer which would be resolved by the foreign_key user_id instead of customer_id
103 104 105 |
# File 'lib/active_resource/associations.rb', line 103 def belongs_to(name, ={}) Builder::BelongsTo.build(self, name, ) end |
#has_many(name, options = {}) ⇒ Object
Specifies a one-to-many association.
Options
[:class_name] Specify the class name of the association. This class name would be used for resolving the association class.
Example for [:class_name] - option
GET /posts/123.xml delivers following response body:
has_many :comments, :class_name => 'myblog/comment' Would resolve those comments into the Myblog::Comment class.
36 37 38 |
# File 'lib/active_resource/associations.rb', line 36 def has_many(name, = {}) Builder::HasMany.build(self, name, ) end |
#has_one(name, options = {}) ⇒ Object
Specifies a one-to-one association.
Options
[:class_name] Specify the class name of the association. This class name would be used for resolving the association class.
Example for [:class_name] - option
GET /posts/123.xml delivers following response body:
has_one :author, :class_name => 'myblog/author' Would resolve this author into the Myblog::Author class.
60 61 62 |
# File 'lib/active_resource/associations.rb', line 60 def has_one(name, = {}) Builder::HasOne.build(self, name, ) end |