0

私はモデル、カテゴリーを持っています。そして、カテゴリが作成されるたびに、新しいデフォルトのsub_categoryを作成したいと思います。しかし、私はそれを行う方法がわかりません。これが私が持っているものです。

class Category < ActiveRecord::Base
    attr_accessible :title, :position

    has_many :sub_categories

    after_create :make_default_sub

    def make_default_sub
      #Sub_Categories.new( :title=>' ');
    end
end
4

1 に答える 1

3

祖先の宝石を使わないのはなぜですか?将来、サブカテゴリが増えると、それらの管理が容易になります。

たとえば、あなたの場合:

class Category < ActiveRecord::Base
    attr_accessible :title, :position

    has_ancestry

    after_create :create_default_subcategory

    def make_default_sub
      children = self.children.new
      children.title = ''
      children.position = 1 # or autogenerated
      children.save!
    end
end

しかし、あなたは説明できますか、なぜあなたはそのような奇妙なデフォルトの振る舞いが必要なのですか?

ありがとう

于 2011-02-04T20:46:34.730 に答える