0

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.

vidur punj
  • 5,019
  • 4
  • 46
  • 65
  • You need to give us more info. What fields are in your models? How are your models related? Are they nested. I think the answer to your question will in part depend on the design of your app. – diasks2 Sep 19 '12 at 11:36
  • 1
    I agree with diasks2. Seems that [AR Validations Guide](http://guides.rubyonrails.org/active_record_validations_callbacks.html) should give you enough info. You could also refer to [AR Associations Guide](http://guides.rubyonrails.org/association_basics.html) for how to relate models to each other. – khustochka Sep 19 '12 at 11:47
  • Right now I am writing the model so they only have few fields like name and details and no association. and I want the association like audio belong to at least album or movie. – vidur punj Sep 19 '12 at 11:47

1 Answers1

1

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

a.s.t.r.o
  • 3,261
  • 5
  • 34
  • 41