1

I didn't define the problem correctly the first time. I tried to make a simple example and failed. Let's try again :)

I have an Address Model that validates the street, city, state, and zip attributes all exist. Then we have a Person Model that has a one to many relationship with Address. We have a student model but it DOESN'T inherit off person, it just has_one (I didn't completely understand how inheritance worked in ruby when I started). We have a Classroom model that also has_on person (again, not inherited.) So the issue is we want to require an address for the student, but not the Classroom. Hopefully this is a little more clear now. Thanks!

2
  • If you do understand how inheritance works now, why Student is still not inheriting from Person ? Commented Nov 13, 2012 at 5:55
  • possible duplicate of Rails - Validate Presence Of Association? Commented Nov 14, 2012 at 3:12

1 Answer 1

1

You can use validates or validates_presence_of in only your Student model.

class Person
  has_many :addresses
end

class Student < Person
  validates :addresses, :presence => true
end

class Teacher < Person
end
Sign up to request clarification or add additional context in comments.

1 Comment

After thinking about it, I could have another model that doesn't validate address and make the Classroom has_one of those. This is acceptable, just took me a lil while to wrap my head around it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.