2010-12-02 70 views
0
我有awesome_nested_set在選擇重複孩子的問題(如標題所說)

awesome_nested_set在複製的孩子選擇

我在表中的以下數據(phpmyadmin的周圍放上領域引號):

id  name   parent_id lft rgt  created_at    updated_at 
"12" "BNW"  NULL  "1"  "8"  "2010-12-01 22:23:36" "2010-12-01 22:23:36" 
"13" "NYSSA"  "12"  "2"  "3"  "2010-12-01 22:23:48" "2010-12-01 22:25:12" 
"14" "BOARDMAN"  "12"  "4"  "5"  "2010-12-01 22:25:28" "2010-12-01 22:25:28" 
"15" "QUINCY"  "12"  "6"  "7"  "2010-12-01 22:25:37" "2010-12-01 22:25:37" 
"16" "WCC"  NULL  "9"  "16" "2010-12-02 18:08:00" "2010-12-02 18:08:00" 
"17" "CALIFORNIA" "16"  "10" "11" "2010-12-02 18:08:08" "2010-12-02 18:08:08" 
"18" "NORTH POWDER" "16"  "12" "13" "2010-12-02 18:08:16" "2010-12-02 18:08:16" 
"19" "ELLENSBURG" "16"  "14" "15" "2010-12-02 18:08:25" "2010-12-02 18:08:25" 

型號:

class DestinationGroup < ActiveRecord::Base 
    acts_as_audited 
    has_one :purchase 
    acts_as_nested_set 
end 

控制器:

class PurchasesController < ApplicationController 
    def new 
    @destination_groups = DestinationGroup.all 
    ... 
    end 
... 

查看:

= f.select :destination_group_id, options_for_select(nested_set_options(@destination_groups) {|i| "#{'-' * i.level} #{i.name}" }) 

如果您需要更多信息,我可以將它張貼,但我認爲這是所有的,以真正的問題有關。

編輯:

我得到這個網站:

<select id="purchase_destination_group_id" name="purchase[destination_group_id]"> 
    <option value="12"> BNW</option> 
    <option value="13">- NYSSA</option> 
    <option value="14">- BOARDMAN</option> 
    <option value="15">- QUINCY</option> 
    <option value="13">- NYSSA</option> 
    <option value="14">- BOARDMAN</option> 
    <option value="15">- QUINCY</option> 
    <option value="16"> WCC</option> 
    <option value="17">- CALIFORNIA</option> 
    <option value="18">- NORTH POWDER</option> 
    <option value="19">- ELLENSBURG</option> 
    <option value="17">- CALIFORNIA</option> 
    <option value="18">- NORTH POWDER</option> 
    <option value="19">- ELLENSBURG</option> 
</select> 

編輯2

這得到怪異:

DestinationGroup.all.collect{|i| "#{'-' * i.level} #{i.name}" } 
=> [" BNW", "- NYSSA", "- BOARDMAN", "- QUINCY", " WCC", "- CALIFORNIA", "- NORTH POWDER", "- ELLENSBURG"] 

這顯然出來只是罰款。

,我在視圖中做:

= "<!-- #{@destination_groups.inspect} -->" 

<!-- 
    [ 
    #<DestinationGroup id: 12, name: "BNW", parent_id: nil, lft: 1, rgt: 8, created_at: "2010-12-01 22:23:36", updated_at: "2010-12-01 22:23:36">, 
    #<DestinationGroup id: 13, name: "NYSSA", parent_id: 12, lft: 2, rgt: 3, created_at: "2010-12-01 22:23:48", updated_at: "2010-12-01 22:25:12">, 
    #<DestinationGroup id: 14, name: "BOARDMAN", parent_id: 12, lft: 4, rgt: 5, created_at: "2010-12-01 22:25:28", updated_at: "2010-12-01 22:25:28">, 
    #<DestinationGroup id: 15, name: "QUINCY", parent_id: 12, lft: 6, rgt: 7, created_at: "2010-12-01 22:25:37", updated_at: "2010-12-01 22:25:37">, 
    #<DestinationGroup id: 16, name: "WCC", parent_id: nil, lft: 9, rgt: 16, created_at: "2010-12-02 18:08:00", updated_at: "2010-12-02 18:08:00">, 
    #<DestinationGroup id: 17, name: "CALIFORNIA", parent_id: 16, lft: 10, rgt: 11, created_at: "2010-12-02 18:08:08", updated_at: "2010-12-02 18:08:08">, 
    #<DestinationGroup id: 18, name: "NORTH POWDER", parent_id: 16, lft: 12, rgt: 13, created_at: "2010-12-02 18:08:16", updated_at: "2010-12-02 18:08:16">, 
    #<DestinationGroup id: 19, name: "ELLENSBURG", parent_id: 16, lft: 14, rgt: 15, created_at: "2010-12-02 18:08:25", updated_at: "2010-12-02 18:08:25"> 
    ] 
--> 

謝謝

+0

你回來的結果是什麼? – karmajunkie 2010-12-02 20:15:51

回答

0

我想通了,我用的是:

# controller 
@destination_groups = DestinationGroup.all 

則:

# view 
= f.select :destination_group_id, options_for_select(nested_set_options(@destination_groups) {|i| "#{'-' * i.level} #{i.name}" }) 

我將其更改爲:

# view 
= f.select :destination_group_id, options_for_select(nested_set_options(DestinationGroup) {|i| "#{'-' * i.level} #{i.name}" }) 

我猜測它是從DestinationGroup.all中複製出來的,反正它的固定版本任何人都會再次遇到這個問題。