Skip to content

Commit

Permalink
Update required column config for children_count
Browse files Browse the repository at this point in the history
In order for this feature to function properly when we add counter_cache
updating on updating (currently it's only for create/delete) we'll need
these values to be consistent in the database.

Without a default of 0 if we tried to `increment_counter` on `nil` we
would run into errors.
  • Loading branch information
graygilmore committed Jan 20, 2016
1 parent a195410 commit 5c72873
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ gem 'awesome_nested_set'
To make use of `awesome_nested_set` your model needs to have 3 fields:
`lft`, `rgt`, and `parent_id`. The names of these fields are configurable.
You can also have optional fields: `depth` and `children_count`. These fields are configurable.
Note that the `childrent_count` column must have `null: false` and `default: 0` to
function properly.

```ruby
class CreateCategories < ActiveRecord::Migration
Expand Down Expand Up @@ -66,7 +68,7 @@ You can pass various options to `acts_as_nested_set` macro. Configuration option
* `left_column`: column name for left boundary data (default: lft)
* `right_column`: column name for right boundary data (default: rgt)
* `depth_column`: column name for the depth data default (default: depth)
* `scope`: restricts what is to be considered a list. Given a symbol, it'll attach _id (if it hasn't been already) and use that as the foreign key restriction. You can also pass an array to scope by multiple attributes. Example: `acts_as_nested_set :scope => [:notable_id, :notable_type]`
* `scope`: restricts what is to be considered a list. Given a symbol, it'll attach `_id` (if it hasn't been already) and use that as the foreign key restriction. You can also pass an array to scope by multiple attributes. Example: `acts_as_nested_set :scope => [:notable_id, :notable_type]`
* `dependent`: behavior for cascading destroy. If set to :destroy, all the child objects are destroyed alongside this object by calling their destroy method. If set to :delete_all (default), all the child objects are deleted without calling their destroy method. If set to :nullify, all child objects will become orphaned and become roots themselves.
* `counter_cache`: adds a counter cache for the number of children. defaults to false. Example: `acts_as_nested_set :counter_cache => :children_count`
* `order_column`: on which column to do sorting, by default it is the left_column_name. Example: `acts_as_nested_set :order_column => :position`
Expand Down
2 changes: 1 addition & 1 deletion spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
t.column :lft, :integer
t.column :rgt, :integer
t.column :depth, :integer
t.column :children_count, :integer
t.column :children_count, :integer, null: false, default: 0
end

create_table :brokens, :force => true do |t|
Expand Down

0 comments on commit 5c72873

Please sign in to comment.