1

I'm using Rails 3.2 and 4.

I have a Survey and I want to validate that each question has between 2 and 5 answers. Survey has_many Questions, Questions has_many Answers and belongs to Survey. Answer belongs to Question.

How would I do this?

1 Answer 1

1

In Question model

validate :answer_count


def answer_count
  self.errors[:base] << "Can have answers only between 2 and 5" if answers.length<1 || answers.length>5
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.