开发者

awesome_nested_set duplicating children in a select

开发者 https://www.devze.com 2023-01-28 09:41 出处:网络
I am having a problem with awesome_nested_set duplicating children in a select (as the title says) I have the following data in the table (phpmyadmin put quotes around the fields):

I am having a problem with awesome_nested_set duplicating children in a select (as the title says)

I have the following data in the table (phpmyadmin put quotes around the fields):

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"

Model:

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

Controller:

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

View:

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

If you need more info I can post it, but I thought this was all that was really relevant to the problem.

EDIT:

I am getting this html:

<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> 

Edit 2

This gets weirder:

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

this obviously comes out just fine.

and I did this in the view:

= "<!-- #{@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开发者_如何学C 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">
  ] 
--> 

Thank you


I figured it out, I was using:

# controller
@destination_groups = DestinationGroup.all

then:

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

I changed it to:

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

I am guessing it was duplicating it from the DestinationGroup.all, anyways its fixed incase anyone runs into this again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号