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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:14:17+00:00 2026-05-14T07:14:17+00:00

I have a controller/model hypothetically named Pets. Pets has the following declarations: belongs_to :owner

  • 0

I have a controller/model hypothetically named Pets. Pets has the following declarations:

belongs_to :owner
has_many :dogs
has_many :cats

Not the best example, but again, it demonstrates what I’m trying to solve. Now when a request comes in as an HTTP POST to http://127.0.0.1/pets, I want to create an instance of Pets. The restriction here is, if the user doesn’t submit at least one dog or one cat, it should fail validation. It can have both, but it can’t be missing both.

How does one handle this in Ruby on Rails? Dogs don’t care if cats exists and the inverse is also true. Can anyone show some example code of what the Pets model would look like to ensure that one or the other exists, or fail otherwise? Remember that dogs and cats are not attributes of the Pets model. I’m not sure how to avoid Pets from being created if its children resources are not available though.

errors.add also takes an attribute, in this case, there is no particular attribute that’s failing. It’s almost a ‘virtual’ combination that’s missing. Parameters could come in the form of cat_name=”bob” and dog_name=”stew”, based on the attribute, I should be able to create a new cat or dog, but I need to know at least one of them exists.

  • 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-14T07:14:18+00:00Added an answer on May 14, 2026 at 7:14 am

    You’re looking for errors.add_to_base. This should do the trick:

    class Pet < ActiveRecord::Base
      belongs_to :owner
      has_many :dogs
      has_many :cats
    
      validate :has_cats_or_dogs
    
      def has_cats_or_dogs
        if dogs.empty? and cats.empty?
          errors.add_to_base("At least one dog or cat required")
        end
      end
    end
    

    If you want to pass cat_name or dog_name to the controller action, it may look like this:

    class PetsController < ApplicationController
      # ...
    
      def create
        @pet = Pet.new(params[:pet])
        @pet.cats.build(:name => params[:cat_name]) if params[:cat_name]
        @pet.dogs.build(:name => params[:dog_name]) if params[:dog_name]
        if @pet.save
          # success
        else
          # (validation) failure
        end
      end
    end
    

    Alternatively, for some more flexibility you can use nested attributes to create new cats and dogs in your controller.

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

Sidebar

Related Questions

I have the following Model and Controller files, and when i visit this url,
I have a controller and model both named account without getting cannot redeclare class.
so i have got two models with each one controller: Model Project has_many Themes
I have controller and model. i am modifying a variable value in a model,
I have Controller: [MySite]\Controllers\DistributionTools\TrackingChannelsController.cs [HttpPost] public void InitTcFirstPageView() { var model = new TcFirstPageModel
I have tried to create a simple controller/model (based on the samples code of
In my controller I have model operations that can return empty results. I've setup
I have a doubt about Model behavior in Spring MVC. I have a controller
I have a controller called articles , which creates the articles model which gets
I have a Rails app with the usual application_controller, and a controller (and 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.