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 9058431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:43:18+00:00 2026-06-16T14:43:18+00:00

I have model Category which has and belongs to many Packages. When a user

  • 0

I have model Category which has and belongs to many Packages. When a user creates a Posting in a Category, it must belong to a Package that is in its category.

Here’s the code for the custom validator:

class PackageValidator < ActiveModel::Validator
  def validate(record)
    record.errors[:package_id] <<
      I18n.t('errors.messages.package.not_in_category') unless
      record.category.packages.include? record.package
  end
end

This works great and all, but it breaks all my rspec tests (with factory_girl) because factory_girl creates a new Package for each Category and Posting I build/create with it.

Here’s my factories:

factory :category do
  sequence(:name) {|n| "Parts#{n}"}
  sequence(:slug) {|n| "parts#{n}"}
  packages {[FactoryGirl.create(:package)]}
end

factory :posting do
  title 'Foo'
  body 'Bar'
  category
  package
end

factory :package do
  sequence(:name) {|n| "Basic#{n}"
  price 0
  photos 5
end

How can I make it all play nicely together?

  • 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-16T14:43:20+00:00Added an answer on June 16, 2026 at 2:43 pm

    You can set the package to category.packages.first in an after(:build) hook.

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

Sidebar

Related Questions

I have a Category model, which has many Products . I would like to
In my code I have a Category model which has many subcategories, and the
I have category model that has a tree structure. In my database I have
I have an article model that has a title, body, author and category fields.
I have a model class 'Market' which has many products: class Market < ActiveRecord::Base
I have the following complex model: Category - has -> List which has ->
I have a model Category which has a FK reference to itself. How can
I have a model question which has a column called category. I have an
I have a Model Category which has name:string categoryId:integer parent_id:integer the categoryId doesnt matter
I have one model User and will be creating another one Category . Both

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.