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

  • SEARCH
  • Home
  • 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 7959831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:38:05+00:00 2026-06-04T04:38:05+00:00

I have a category model. I am using ancestry gem to make it like

  • 0

I have a category model. I am using ancestry gem to make it like a tree structure so that we can have sub categories. I have a simple factory for category that looks like this

factory :category do
  sequence(:name) { |n| "Category #{n}"}
end

I have another model business which has a main_category and a sub_category. Here is my factory for the business

factory :business do
  sequence(:name) { |n| "businnes#{n}"}
  sequence(:email) { |n| "busixy#{n}@example.com"}
  sequence(:website) { |n| "www.example#{n}.com"}
  association(:main_sub_category)
  main_category          {Business.main_sub_category.parent}

end

What I am trying to do here is to assign main_sub_category of the business a new category which is a sub_category and to assign the parent of that category to main_category of the business.

To get this I created a new factory called sub_category
factory :category do
sequence(:name) { |n| “Category #{n}”}
factory :subcategory do |sub|
sub.parent factory :category
end
end

When I try the above code I get this error

Factory already registered: category (FactoryGirl::DuplicateDefinitionError)

I tried another way to do it

 factory :category do
      sequence(:name) { |n| "Category #{n}"}
      factory :subcategory do |sub|
        sub.parent {FactoryGirl.create(:category)}
      end
    end

This way it works but when I create a business object using the factory

@business = FactoryGirl.create :business

It gives me validation error

ActiveRecord::RecordInvalid: Validation failed: Name has already been taken

I have a uniqueness validation on category names.

Any has had this kind of a recursive factory issue kind help.

Thanks

  • 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-06-04T04:38:06+00:00Added an answer on June 4, 2026 at 4:38 am

    Got it resolved, for some mysterious reason I don’t understand my test db had a already had a single entry for a category with name Category1 which was creating the issue, manually deleted that row using mysql console and everthing works now.

    I still wonder why database wasn’t clear, since I never write to test db directly its the testcase that do it.. :S

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

Sidebar

Related Questions

I have category model that has a tree structure. In my database I have
I have ancestry for my category model. Im using slug gem. Currently i have
I have written a simple Magento plugin that simply lists all sub categories that
Using: Rails 3.1.1 I have a controller/model/view called Category that is built up on
In my project I am using django-mptt for categories. My model: class Category(models.model): name
I have a model using Django-MPTT that also has a field for using a
I'm using CakePHP 2.0. I have 2 models/controllers (Category/Coupon): // File: Model/Coupon.php // Table
I have got a category - model which i am using it for my
I have a auction model and a category model. Auction belongs_to category and category
I have a Category and a Post model, with each Post belonging to a

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.