# Is there a simply way to check if two models are associated?

**URL:** <https://www.ruby-forum.com/t/is-there-a-simply-way-to-check-if-two-models-are-associated/181655>\
**Category:** Rails\
**Created:** [January 15, 2010, 11:13am UTC](https://www.ruby-forum.com/t/is-there-a-simply-way-to-check-if-two-models-are-associated/181655 "2010-01-15T11:13:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![85niner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/8/a4c791/32.png) [@85niner](https://www.ruby-forum.com/u/85niner)\
**Post date:** [January 15, 2010, 11:13am UTC](https://www.ruby-forum.com/t/is-there-a-simply-way-to-check-if-two-models-are-associated/181655/1 "2010-01-15T11:13:19Z")

</div>

Hey guys,

I spent last night trawling through the rails API looking for some  
sort of @object1.associated\_with?(@object2) and couldn’t find  
anything, but it seems so fundamental… Does anything like this  
exist?

if not, i might build a simple plugin/gem to mimic this behaviour.

Mark

---

<div class="post-metadata">

**Author:** ![85niner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/8/a4c791/32.png) [@85niner](https://www.ruby-forum.com/u/85niner)\
**Post date:** [January 15, 2010, 2:58pm UTC](https://www.ruby-forum.com/t/is-there-a-simply-way-to-check-if-two-models-are-associated/181655/2 "2010-01-15T14:58:03Z")

</div>

Mark Y g wrote:

> Hey guys,
> 
> I spent last night trawling through the rails API looking for some  
> sort of @object1.associated\_with?(@object2) and couldn’t find  
> anything, but it seems so fundamental… Does anything like this  
> exist?

Sure. Assuming you have your associations set up properly (for example,  
User has\_many :posts), you could do @post.user == @user or  
@user.posts.include? @post.

> if not, i might build a simple plugin/gem to mimic this behaviour.
> 
> Mark

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
