Skip to content

Commit

Permalink
Added :optional information on README
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Feb 15, 2009
1 parent c66a01c commit 8b5b2c5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions README
Expand Up @@ -347,6 +347,35 @@ When using polymorphic associations, you get some free helpers:
Polymorphic controller is another great idea by James Golick and he also uses
that on resource_controller.

Optional belongs to
-------------------

Let's take another break from Projects. Let's suppose that we are now building
a store, which sell products.

On the website, we can show all products, but also products scoped to
categories, brands, users, etc. In this case case, the association is optional,
and we deal with it in the following way:

class ProductsController < InheritedResources::Base
belongs_to :category, :brand, :user, :polymorphic => true, :optional => true
end

This will handle all those urls properly:

/products/1
/categories/2/products/5
/brands/10/products/3
/user/13/products/11

This is treated as a special type of polymorphic associations, thus all helpers
are available. As you expect, when no parent is found, the helpers return:

parent? #=> false
parent_type #=> nil
parent_class #=> nil
parent #=> nil

Singletons
----------

Expand Down
4 changes: 2 additions & 2 deletions lib/inherited_resources/class_methods.rb
Expand Up @@ -130,8 +130,8 @@
#
# = optional polymorphic associations
#
# Let's take another vacation from our ProjectsController. Let's suppose we are
# building an store, which has products to sell.
# Let's take another break from ProjectsController. Let's suppose we are
# building a store, which sell products.
#
# On the website, we can show all products, but also products scoped to
# categories, brands, users. In this case case, the association is optional, and
Expand Down

0 comments on commit 8b5b2c5

Please sign in to comment.