Dependent_collection_select in nested form

11 views
Skip to first unread message

Brightspark

unread,
Jun 25, 2010, 10:07:23 PM6/25/10
to dependent_select
Hi there.

I want to start by saying I really love your gem.
But I am confused about a things when the the situation gets a little
more complicated.

I have the following scenario:

- I have Stores and Products.
- Each Product belongs to a Category and has a Product Price
(:retail_price and :sale_price) for each Store.
- I have Sales, and each Sale belong to a Category.
- Each Sale has Sale Items.
- A Sale Item is a Product being sold at the :sale_price in a Store.
- Every Store must participate in a Sale.
- Not every Store stocks the same Product, so a Store must choose a
Product to put on Sale in the same Product Category.

The code for all this can be found here: http://gist.github.com/453676

The New Sale form lets you choose a Product Category for the sale.
And a Sale Category.
The Product Category determines the list of products shown for each
Sale Item.
The Sale Item belongs to a Store, therefore a Sale has one Sale Item
for each Store

What I'm confused about is:

1. Which model to put the delegate instruction on.
2. How to restrict the Product collection for each Sale Item by Store
(so only the Products for that Store in the selected Product Category
are displayed).

I'm hoping you can throw some light, any assistance is appreciated.

Thanks for a great gem.

Brightspark

unread,
Jun 26, 2010, 12:58:52 AM6/26/10
to dependent_select
OK I've added some named_scopes and convenience methods on Product and
ProductPrice.

The gist has been updated:
- Added a named_scope to ProductPrice for fetching by_store.
- Added a named_scope to Product for fetching by_category.
- Added a convenience method to Product for fetching by_store.
- Added a convenience method to Product for fetching
by_store_and_category.
- Corrected form to include the store_id in the hidden field rather
than country_id. This was a copy paste error.

So I can now fetch Products.by_store_and_category. The method is a
little ugly, but it works. Any advice on how to achieve this more
elegantly (or the Ruby way) would be much appreciated.

How do I now get the (multiple) dependent_collection_select(s) to
update their collections using this method when the product_category
(collection_select) is changed in the New Sale form?

kikito

unread,
Jun 26, 2010, 5:26:10 AM6/26/10
to dependent_select
Hi Brightspark.

You've got a complex model relationship there! It's quite difficult to
follow for someone that doesn't know the problem well.

In the future, consider removing any parts that are not essential to
understanding the problem from your gists.

I have not used dependent_collection_select with formtastic. The
chances for it working "out of the box" with it are ... very low. You
will probably have to monkeypatch Formtastic in order to accept
dependent_collection_select as a valid order; I'm not sure about how.
This could be what is giving you the "wrong number of arguments (6 for
2)." error.

Dependent you can make lots of dependent selects depend on the same
field. This just means that more than one javascript "observer" will
be linked to the "observed" field. This should make no difference.

The difference however is that dependent selects work on 1 dependency
only. I'm not sure, but it seems that in your example you need your
dependent selects have 2 or 3 dependencies instead of one. They are
not prepared for that.

You *could* make them depend on a hidden field that concatenated two
values, but that would be ugly, inelegant and error prone. If your
selects depend on two or more fields, then I recommend you not to use
my gem; update the relevant portions using AJAX instead. You will have
to create some partial views and add some divs, as well as modify/add
1 controller action, but the result will be more satisfying (and rail-
y)

One last thing: when adding conditions "as a string", you should
include your table names. So, instead of this:

:conditions => ["type = ?", "Product"]

I recommend you to do this:

:conditions => ["categories.type = ?", "Product"]

Otherwise you will get a SQL error when you try to use that named
scope in joins with other models that also have a "type" column.

Regards, and good luck!
Reply all
Reply to author
Forward
0 new messages