### Install Ancestry Gem Source: https://github.com/stefankroes/ancestry/blob/master/README.md Executes the bundle install command to download and install the gems listed in the Gemfile, including the newly added Ancestry gem. ```bash $ bundle install ``` -------------------------------- ### Applying Depth Constraints Source: https://github.com/stefankroes/ancestry/blob/master/README.md Examples demonstrating how to apply depth constraints using named scopes or options when querying ancestors, descendants, or paths. Requires depth caching to be enabled. ```Ruby node.ancestors(:from_depth => -6, :to_depth => -4) ``` ```Ruby node.path.from_depth(3).to_depth(4) ``` ```Ruby node.descendants(:from_depth => 2, :to_depth => 4) ``` ```Ruby node.subtree.from_depth(10).to_depth(12) ``` -------------------------------- ### Arranging Subtree Source: https://github.com/stefankroes/ancestry/blob/master/README.md Examples showing how to use the `arrange` method on a subtree relation to structure nodes into a nested hash format for easier navigation. ```Ruby TreeNode.find_by(:name => 'Crunchy').subtree.arrange ``` ```Ruby TreeNode.find_by(:name => 'Crunchy').subtree.arrange(:order => :name) ``` -------------------------------- ### Run Ancestry Gem Tests Source: https://github.com/stefankroes/ancestry/blob/master/README.md Series of Bash commands to clone the ancestry gem repository, set up the test environment (copying database config, installing gems with bundle and appraisal), and run the test suite using appraisal and rake. ```Bash git clone git@github.com:stefankroes/ancestry.git cd ancestry cp test/database.example.yml test/database.yml bundle appraisal install # all tests appraisal rake test # single test version (sqlite and rails 5.0) appraisal sqlite3-ar-50 rake test ``` -------------------------------- ### Configuring Ancestry Column in Postgres Source: https://github.com/stefankroes/ancestry/blob/master/README.md Examples of Rails migration syntax for configuring the `ancestry` column in PostgreSQL to ensure correct sorting and index usage, particularly for ASCII-based comparisons. ```Ruby t.string "ancestry", collation: 'C', null: false t.index "ancestry" ``` ```Ruby t.string "ancestry", null: false t.index "ancestry", opclass: :varchar_pattern_ops ``` -------------------------------- ### Arranging Subtree for Serialization Source: https://github.com/stefankroes/ancestry/blob/master/README.md Examples using `arrange_serializable` to structure a subtree into a hash of arrays, suitable for serialization (e.g., to JSON). Includes options for ordering and custom serialization logic using blocks. ```Ruby TreeNode.arrange_serializable(:order => :name) ``` ```Ruby TreeNode.arrange_serializable { |parent, children| MySerializer.new(parent, children: children) } ``` ```Ruby TreeNode.arrange_serializable do |parent, children| { my_id: parent.id, my_children: children } end ``` -------------------------------- ### Add Ancestry Column Migration Code Source: https://github.com/stefankroes/ancestry/blob/master/README.md Defines the database migration to add the 'ancestry' string column and index to the target table, including examples for PostgreSQL and MySQL collations. ```ruby class AddAncestryToTable < ActiveRecord::Migration[6.1] def change change_table(:table) do |t| # postgres t.string "ancestry", collation: 'C', null: false t.index "ancestry" # mysql t.string "ancestry", collation: 'utf8mb4_bin', null: false t.index "ancestry" end end end ``` -------------------------------- ### Get Ancestor IDs - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves an array of IDs for all ancestor nodes, starting from the parent up to the root. This method provides the path of IDs leading from the root to the current node's parent. It's useful for operations involving the node's lineage. ```Ruby ancestor_ids ``` -------------------------------- ### Querying Nodes using Ancestry Scopes in Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Demonstrates how to use navigation methods and named scopes returned by the Ancestry gem to perform queries on nodes, applying additional conditions, ordering, and limits before retrieving or checking for existence. Examples include filtering children, iterating over a subtree, and counting descendants. ```Ruby node.children.where(:name => 'Mary').exists? node.subtree.order(:name).limit(10).each { ... } node.descendants.count ``` -------------------------------- ### Get Subtree IDs - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves an array of IDs for all nodes in the subtree rooted at the current node, including the current node itself. This method provides the IDs for the node and all its descendants. It's useful for operations on the entire branch. ```Ruby subtree_ids ``` -------------------------------- ### Filter STI Results - ActiveRecord Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md An example of using ActiveRecord's where method to filter results by type when using Single Table Inheritance (STI). This snippet shows how to limit the returned nodes from navigation methods to a specific subclass. ```Ruby where(:type => "ChildClass") ``` -------------------------------- ### Get Path IDs - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves an array of IDs for all nodes in the path from the root node to the current node, including both the root and the current node. This method provides the lineage of the node as a sequence of IDs. It's useful for tracing the node's ancestry. ```Ruby path_ids ``` -------------------------------- ### Get Sibling IDs - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves an array of IDs for all sibling nodes, including the current node itself. This method returns the IDs of all nodes that share the same parent as the current node. It's useful for operations on nodes at the same level. ```Ruby sibling_ids ``` -------------------------------- ### Get Child IDs - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves an array of IDs for all direct child nodes of the current node. This method returns the IDs of nodes immediately below the current node in the hierarchy. It's useful for iterating over or querying direct children. ```Ruby child_ids ``` -------------------------------- ### Get Root ID - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves the ID of the root node of the tree containing the current node. For the root node itself, this method returns its own ID. It provides a quick way to identify the top-level node in any branch. ```Ruby root_id ``` -------------------------------- ### Get Parent ID - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves the ID of the direct parent node in the tree structure. This is typically a foreign key column used by the ancestry gem to link nodes. Returns nil for the root node. ```Ruby parent_id ``` -------------------------------- ### Get Descendant IDs - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves an array of IDs for all descendant nodes, including children, grandchildren, and so on. This method returns the IDs of all nodes in the subtree rooted at the current node, excluding the current node itself. It's useful for operations on the entire branch below the node. ```Ruby descendant_ids ``` -------------------------------- ### Get Indirect Descendant IDs - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Retrieves an array of IDs for indirect descendant nodes, which are descendants excluding direct children. This method provides IDs for grandchildren and all subsequent generations below the current node. It's useful for targeting nodes further down the hierarchy. ```Ruby indirect_ids ``` -------------------------------- ### Run Database Migration Source: https://github.com/stefankroes/ancestry/blob/master/README.md Executes the database migration to apply the changes defined in the migration file, creating the 'ancestry' column in the database table. ```bash $ rake db:migrate ``` -------------------------------- ### Run Database Migrations Source: https://github.com/stefankroes/ancestry/blob/master/README.md Bash command to execute pending database migrations, applying schema changes like adding or removing columns. ```Bash $ rake db:migrate ``` -------------------------------- ### Creating Nodes using Ancestry Scopes in Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Illustrates how to leverage the scopes returned by Ancestry's navigation methods and class-level helpers to create new nodes. This includes creating children and siblings relative to an existing node or using class methods with a node ID. ```Ruby node.children.create node.siblings.create! TestNode.children_of(node_id).new TestNode.siblings_of(node_id).create ``` -------------------------------- ### Configure Ancestry Initializer Source: https://github.com/stefankroes/ancestry/blob/master/README.md Sets default configuration options for the Ancestry gem within a Rails initializer file, such as specifying the ancestry format. ```ruby # config/initializers/ancestry.rb # use the newer format Ancestry.default_ancestry_format = :materialized_path2 # Ancestry.default_update_strategy = :sql ``` -------------------------------- ### Create Tree Node with Parent Source: https://github.com/stefankroes/ancestry/blob/master/README.md Demonstrates how to create a new tree node and assign its parent node directly using the `parent` attribute during the creation process. ```ruby TreeNode.create! :name => 'Stinky', :parent => TreeNode.create!(:name => 'Squeeky') ``` -------------------------------- ### Configure PostgreSQL Binary Output Format Source: https://github.com/stefankroes/ancestry/blob/master/README.md Alters a PostgreSQL database setting (`bytea_output`) to 'escape' for improved readability of binary data when the ancestry column is defined as binary. ```SQL ALTER DATABASE dbname SET bytea_output to 'escape'; ``` -------------------------------- ### Generate Ancestry Migration Source: https://github.com/stefankroes/ancestry/blob/master/README.md Generates a Rails migration file to add the 'ancestry' string column with an index to a specified database table. ```bash $ rails g migration add_[ancestry]_to_[table] ancestry:string:index ``` -------------------------------- ### Populate Ancestry Column from Parent IDs Source: https://github.com/stefankroes/ancestry/blob/master/README.md Ruby code to run in the Rails console to populate the `ancestry` column based on an existing `parent_id` column when migrating from a different nested set plugin. Includes optional steps for depth cache and integrity check. ```Ruby Model.build_ancestry_from_parent_ids! # Model.rebuild_depth_cache! Model.check_ancestry_integrity! ``` -------------------------------- ### Add Ancestry Gem to Gemfile Source: https://github.com/stefankroes/ancestry/blob/master/README.md Adds the 'ancestry' gem to your project's Gemfile, making it available for use within your Rails application. ```ruby # Gemfile gem 'ancestry' ``` -------------------------------- ### Sorting Nodes by Ancestry Source: https://github.com/stefankroes/ancestry/blob/master/README.md Demonstrates using the `sort_by_ancestry` class method to sort an array of nodes in preorder traversal order. Note that sibling order depends on the input array. ```Ruby TreeNode.sort_by_ancestry(array_of_nodes) ``` -------------------------------- ### Define Binary Ancestry Column in Rails Migration (MySQL) Source: https://github.com/stefankroes/ancestry/blob/master/README.md Defines a `binary` column named "ancestry" with a limit of 3000 and a null constraint in a Rails database migration, discussed in the context of MySQL storage options. Also adds an index. ```Ruby t.binary "ancestry", limit: 3000, null: false t.index "ancestry" ``` -------------------------------- ### Define MySQL String Ancestry Column with Binary Collation Source: https://github.com/stefankroes/ancestry/blob/master/README.md Defines a `string` column named "ancestry" in a Rails migration, specifying the `'binary'` collation for MySQL and setting a limit and null constraint. This acts similarly to a binary column. An index is also added. ```Ruby t.string "ancestry", collate: 'binary', limit: 3000, null: false t.index "ancestry" ``` -------------------------------- ### Define Binary Ancestry Column in Rails Migration Source: https://github.com/stefankroes/ancestry/blob/master/README.md Defines a `binary` column named "ancestry" with a limit of 3000 and a null constraint in a Rails database migration. Also adds an index to this column. ```Ruby t.binary "ancestry", limit: 3000, null: false t.index "ancestry" ``` -------------------------------- ### Enable Ancestry with Depth Caching in Rails Model Source: https://github.com/stefankroes/ancestry/blob/master/README.md Ruby code snippet showing how to add the `has_ancestry` macro to a Rails model, enabling the ancestry gem's functionality and specifically turning on depth caching by setting `cache_depth: true`. ```Ruby # app/models/[model.rb] class [Model] < ActiveRecord::Base has_ancestry cache_depth: true end ``` -------------------------------- ### Migrate Ancestry Format to materialized_path2 Source: https://github.com/stefankroes/ancestry/blob/master/README.md Ruby code to update existing ancestry values in the database when migrating from the `:materialized_path` format to `:materialized_path2`. It updates child nodes by adding delimiters and sets root nodes, then changes the column to be non-nullable. ```Ruby klass = YourModel # set all child nodes klass.where.not(klass.arel_table[klass.ancestry_column].eq(nil)).update_all("#{klass.ancestry_column} = CONCAT('#{klass.ancestry_delimiter}', #{klass.ancestry_column}, '#{klass.ancestry_delimiter}')") # set all root nodes klass.where(klass.arel_table[klass.ancestry_column].eq(nil)).update_all("#{klass.ancestry_column} = '#{klass.ancestry_root}'") change_column_null klass.table_name, klass.ancestry_column, false ``` -------------------------------- ### Rebuild Ancestry Depth Cache Source: https://github.com/stefankroes/ancestry/blob/master/README.md Ruby code to be run in the Rails console or a script to rebuild the `ancestry_depth` values for all records in the model after adding the depth cache column or migrating data. ```Ruby Model.rebuild_depth_cache! ``` -------------------------------- ### Define MySQL String Ancestry Column with utf8mb4_bin Collation Source: https://github.com/stefankroes/ancestry/blob/master/README.md Defines a `string` column named "ancestry" in a Rails migration, specifying the `'utf8mb4_bin'` collation for MySQL and setting a null constraint. This is the currently suggested way for MySQL storage. An index is also added. ```Ruby t.string "ancestry", collation: 'utf8mb4_bin', null: false t.index "ancestry" ``` -------------------------------- ### Add MySQL Column with ASCII Character Set Source: https://github.com/stefankroes/ancestry/blob/master/README.md Adds a `VARCHAR` column named "ancestry" with a length of 2700 to a MySQL table using an `ALTER TABLE` statement, specifying the `ascii` character set. ```SQL ALTER TABLE table ADD COLUMN ancestry VARCHAR(2700) CHARACTER SET ascii; ``` -------------------------------- ### Rails Migration to Add ancestry_depth Column Source: https://github.com/stefankroes/ancestry/blob/master/README.md Rails migration code that adds an integer column named "ancestry_depth" with a default value of 0 to a specified table, enabling depth caching for the ancestry gem. ```Ruby class AddDepthCacheToTable < ActiveRecord::Migration[6.1] def change change_table(:table) do |t| t.integer "ancestry_depth", default: 0 end end end ``` -------------------------------- ### Generate Migration to Remove parent_id Column Source: https://github.com/stefankroes/ancestry/blob/master/README.md Bash command using the Rails generator to create a new migration file specifically for removing the `parent_id` column from a specified table after migrating to the ancestry gem. ```Bash $ rails g migration remove_parent_id_from_[table] ``` -------------------------------- ### Check if Root - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Checks if the current node is the root node of its tree. This boolean method returns true if the node has no parent (parent_id is nil). It's a fundamental check for tree navigation logic. ```Ruby is_root? ``` -------------------------------- ### Check for Siblings - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Checks if the current node has any sibling nodes (excluding itself). This boolean method returns true if there are other nodes with the same parent. It's a quick check for nodes at the same level. ```Ruby has_siblings? ``` -------------------------------- ### Rails Migration to Remove parent_id Column Source: https://github.com/stefankroes/ancestry/blob/master/README.md Rails migration code that defines the `change` method to remove the "parent_id" column of type integer from a specified table. ```Ruby class RemoveParentIdFromToTable < ActiveRecord::Migration[6.1] def change remove_column "table", "parent_id", type: :integer end end ``` -------------------------------- ### Check for Children - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Checks if the current node has any direct child nodes. This boolean method returns true if the node has one or more children. It's a quick check before attempting to access children. ```Ruby has_children? ``` -------------------------------- ### Add has_ancestry to Model Source: https://github.com/stefankroes/ancestry/blob/master/README.md Adds the `has_ancestry` macro to an ActiveRecord model class, enabling the model to function as a tree structure. ```ruby # app/models/[model.rb] class [Model] < ActiveRecord::Base has_ancestry end ``` -------------------------------- ### Check for Ancestors - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Checks if the current node has any ancestor nodes. This boolean method returns true if the node is not the root node. It's a quick way to determine if a node is part of a deeper branch. ```Ruby ancestors? ``` -------------------------------- ### Check for Parent - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Checks if the current node has a direct parent. This is a boolean method that returns true if parent_id is not nil, indicating the node is not the root. Useful for conditional logic based on node position. ```Ruby has_parent? ``` -------------------------------- ### Check if Root Of - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Checks if the current node is the root of the subtree containing a specified target node. This method takes another node object as an argument. It returns true if the target node's root is the current node. ```Ruby root_of? ``` -------------------------------- ### Check if In Subtree Of - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Determines if the current node is within the subtree rooted at a specified target node. This method takes another node object as an argument. It returns true if the current node is the target node or one of its descendants. ```Ruby in_subtree_of? ``` -------------------------------- ### Check if Ancestor Of - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Determines if the current node is an ancestor of a specified target node. This method takes another node object as an argument. It returns true if the target node is a descendant of the current node. ```Ruby ancestor_of? ``` -------------------------------- ### Check if Parent Of - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Determines if the current node is the direct parent of a specified target node. This method takes another node object as an argument. It returns true if the target node's parent is the current node. ```Ruby parent_of? ``` -------------------------------- ### Check if Sibling Of - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Determines if the current node is a sibling of a specified target node. This method takes another node object as an argument. It returns true if both nodes share the same parent. ```Ruby sibling_of?(node) ``` -------------------------------- ### Check if Child Of - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Determines if the current node is a direct child of a specified target node. This method takes another node object as an argument. It returns true if the current node's parent is the target node. ```Ruby child_of? ``` -------------------------------- ### Check if Descendant Of - Ancestry Ruby Source: https://github.com/stefankroes/ancestry/blob/master/README.md Determines if the current node is a descendant of a specified target node. This method takes another node object as an argument. It returns true if the current node is located within the subtree rooted at the target node. ```Ruby descendant_of? ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.