views:

60

answers:

1

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.

A: 

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.

molf
Thanks for giving me a place to start looking. Mind if I ask how the Pets controller looks in this case though? Am I trying to create a new cat instance and a new dog instance if a param exists and letting this worry about the rest?
randombits
Yes, let your model worry about the validation. I've included a simple controller example.
molf
Just what I was looking for, thanks so much for helping me understand the problem better.
randombits
This code is all sorts of screwy to me. Aside from the syntax errors, `:belongs_to owner` should be `belongs_to :owner`, why do you have a model with a pluralized name, `Pets`? Also, `has_many :cats` suggests that there is a `Cat` model mapped to a `cats` table with a `pet_id`. This makes no sense... A `Cat` **is** a `Pet`. Perhaps it is just a different `PetType`? Do you understand polymorphic associations?
macek
@macek: Thanks for your feedback. I copied the code from the question, hence the errors; updated now. I agree with the issues you take with the modelling, but I'm not sure it's particularly relevant to the question itself (the OP mentioned that the cat/dog/pet thing is hypothetic anyway).
molf