# Validating an association

**URL:** <https://www.ruby-forum.com/t/validating-an-association/146827>\
**Category:** Rails\
**Created:** [August 25, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/validating-an-association/146827 "2008-08-25T23:31:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 25, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/validating-an-association/146827/1 "2008-08-25T23:31:37Z")

</div>

I have two models, A and B, related to one another by a many to many  
association and I want to validate an object of model B as it’s being  
added to the association based on both its own attributes and  
attributes of the particular object of model A that it’s being  
associated with.

I’ve realized that I can’t do this if the association is represented  
by a habtm relationship since (as far as I can tell - please tell me  
if I’m wrong) there’s no way to refer to the A object in my validation  
method for B.

Has many :through looks like a better approach here, since I can  
define the validation method on the join model itself. However this  
raises a question. I can’t create a has many :through association  
unless the objects on both ends of the association are already saved  
(right?). But I don’t want B to be saved if the association is going  
to be invalid.

Does this imply that the only way to handle this is to use a  
transaction to wrap the saving of B and the creation of the  
association? Or is there a smarter way to do this?

---

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 26, 2008, 6:07am UTC](https://www.ruby-forum.com/t/validating-an-association/146827/2 "2008-08-26T06:07:01Z")

</div>

MarkMT wrote:

> I have two models, A and B, related to one another by a many to many  
> association and I want to validate an object of model B as it’s being  
> added to the association based on both its own attributes and  
> attributes of the particular object of model A that it’s being  
> associated with.
> 
> I’ve realized that I can’t do this if the association is represented  
> by a habtm relationship since (as far as I can tell - please tell me  
> if I’m wrong) there’s no way to refer to the A object in my validation  
> method for B.

You could arrange to get access to the A object using something like:

class A \< ActiveRecord::Base  
has\_and\_belongs\_to\_many bs, :before\_add =\> :set\_b\_current\_a  
def set\_b\_current\_a(b) b.current\_a = self end  
end

class B \< ActiveRecord::Base  
has\_and\_belongs\_to\_many as  
attr\_accessor :current\_a  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 26, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/validating-an-association/146827/3 "2008-08-26T20:26:22Z")

</div>

Thanks, that’s pretty interesting.

Mark.
