Rails 自关联该怎么写?

syutran · January 06, 2013 · Last by wxianfeng replied at January 06, 2013 · 4527 hits

我不知道大家在建立分类表时是不是只用一个婊子。一开始我用了五个婊子,发现就是用十个婊子也不够,于是干脆只用一个吧。

1 class CreateCategories < ActiveRecord::Migration 2 def change 3 create_table :categories do |t| 4 t.string :title
5 t.integer :sup_category 6 t.string :tags
7 t.integer :user_id
8 t.text :discription
9 10 t.timestamps
11 end
12 end
13 end

可是不我知道如何建立自关联。谷歌了半天,好像说是再建一个婊子用,没必要吧?我不知道该咋办捏,也不知道它的 index 如何搞个目录树出来。@元芳,你怎么看?

什么子?

@doitian 老弟,虽然我搞了个九级英语的证书,但您这个 github 还是让我头肿。Thank you ...

为什么出现了 婊子

controllers def new @category = current_user.categories.new @categories = current_user.categories.all

respond_to do |format| format.html # new.html.erb format.json { render json: @category } end

new.html.erb

<%= f.collection_select :parent_id, @categories, :id, :title,:include_blank => true %>

报错: NoMethodError in Categories#create

undefined method `map' for nil:NilClass

估计是 @categories 不合 collection_select 的要求,大伙给的例子多是 Category.all,那是所有的类,俺想要某个人单独的类。

搞了一下午,天还没有亮。

同意楼上 用 NestedSet 模型吧,这个模型太棒了。

You need to Sign in before reply, if you don't have an account, please Sign up first.