Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6118997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:30:05+00:00 2026-05-23T15:30:05+00:00

im have model Category. class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column =>

  • 0

im have model Category.

class Category < ActiveRecord::Base
  has_ancestry :cache_depth => true, :depth_cache_column => :depth
end

Category have field name. Im want to build a factory for category with depth level 2.
When this factory will be invoked it must build category level 2 and have parent category with level 1.
How to do this?
Im trying various tricks, all dont work. Im stop at something like that

Factory.define :category do |f|
  f.name                        { Faker::Lorem.word }
  f.parent                      { Factory.create(:category) }
end

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:30:05+00:00Added an answer on May 23, 2026 at 3:30 pm

    Maybe you can create another factory, which has no parent.
    And if you want to have you’re 2 level category you do:

    Factory(:category_level2)
    

    The factory definition could be something like that:

    Factory.define :category do |f|
      f.name   { Faker::Lorem.word }
    end
    
    Factory.define :category_level2, :parent => :category do |f|
      f.parent { Factory.create(:category) }
    end
    

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have a model class ServiceRequest < ActiveRecord::Base has_many :services accepts_nested_attributes_for :services ... end
I have 2 models: class Mission < ActiveRecord::Base belongs_to :category end class Category <
I have following models: class Category < ActiveRecord::Base has_many :items default_scope where(:enabled => true,
I have 2 models: class Video < ActiveRecord::Base belongs_to :categories, :foreign_key => category, :class_name
class Book < ActiveRecord::Base has_and_belongs_to_many :categories has_and_belongs_to_many :users end class Category < ActiveRecord::Base has_and_belongs_to_many
Imagine you have this model: class Category(models.Model): node_id = models.IntegerField(primary_key = True) type_id =
I have 2 models which have a has_and_belongs_to_many relation: class Category < ActiveRecord::Base has_and_belongs_to_many
I have a simple model with news and categories: class Category(models.Model): name = models.CharField()
I have the following Django 1.2 models: class Category(models.Model): name = models.CharField(max_length=255) class Article(models.Model):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.