Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1ruby-1.9.3

Validation to model either this or that but must have one Rails?


I have 3 Models

class Audio
end

class Album
end

class Movie
end

I want to have validation in Audio model.That while creating Audio the server should validate that the audio is linked with the album or movie or both. But both the fields selecting models in the from shouldn't be empty while submit.


Solution

  • Album should be linked with another type, lets say "Media". This allows you to have a field on "Media" which can be either "movie" or "audio".

    Take a look at classy_enum on how to enforce the types: https://github.com/beerlington/classy_enum