views:

181

answers:

0

I'm using Awesome Nested Sets and Formtastic within Rails and am having trouble listing the nester set records in a structured order within a lookup (combo) box.

I'd list to show the lookup box like so:

-Parent1
--P1Child1
--P1Child2
-Parent2
--P2Child1
--P2Child2
--P2Child3

etc...

Origionally the lookup box was displaying the results ordered by the id of the record, displaying the records in order of creation.

My Category table looks like this:

t.string   "name"
t.integer  "parent_id"
t.integer  "lft"
t.integer  "rgt"
t.datetime "created_at"
t.datetime "updated_at"

and my form uses standard formtastic formatting (using HAML):

- semantic_form_for [@business] do |form|
  - form.inputs do
    = form.input :category

which outputs

<select name="business[category_id]" id="business_category_id"><option value=""/>
          <option value="8">Parent1</option>
          <option value="9">P1Child2</option>
          <option value="10">P1Child1</option>
          <option value="12">P1Child3</option>
          <option value="11">Parent2</option>
          <option value="13">P2Child2</option>
          <option value="14">P2Child1</option></select>

I have a default scope ordering by:

lft, parent_id ASC

this allows me to format the sort under their parents but not in alphabetical order, which is where my ordering falls over

my sample data goes like this:

+----+----------+-----------+------+------+---------------------+---------------------+
| id | name     | parent_id | lft  | rgt  | created_at          | updated_at          |
+----+----------+-----------+------+------+---------------------+---------------------+
|  8 | Parent1  |      NULL |    1 |    8 | 2010-05-04 22:07:33 | 2010-05-04 22:07:33 | 
|  9 | P1Child2 |         8 |    2 |    3 | 2010-05-04 22:07:50 | 2010-05-04 22:07:50 | 
| 10 | P1Child1 |         8 |    4 |    5 | 2010-05-04 22:08:01 | 2010-05-04 22:08:01 | 
| 11 | Parent2  |      NULL |    9 |   14 | 2010-05-04 22:08:28 | 2010-05-04 22:08:28 | 
| 12 | P1Child3 |         8 |    6 |    7 | 2010-05-04 22:08:40 | 2010-05-04 22:08:40 | 
| 13 | P2Child2 |        11 |   10 |   11 | 2010-05-04 22:08:53 | 2010-05-04 22:08:53 | 
| 14 | P2Child1 |        11 |   12 |   13 | 2010-05-04 22:09:04 | 2010-05-04 22:09:04 | 
+----+----------+-----------+------+------+---------------------+---------------------+

Can anyone advise?

Thanks in advance