# How to associate one model from 2 models

**URL:** <https://www.ruby-forum.com/t/how-to-associate-one-model-from-2-models/154895>\
**Category:** Rails\
**Created:** [December 4, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/how-to-associate-one-model-from-2-models/154895 "2008-12-04T22:05:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [December 4, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/how-to-associate-one-model-from-2-models/154895/1 "2008-12-04T22:05:41Z")

</div>

I know, I have probably a wrong design. Just need your point of view on  
that.

Here is

Police has many products  
Police has many transactions  
Products has many transactions

I need to get a transaction with the combination of police and product.

Is it feasible to defines associations to do that. I know that’s weird  
a bit. I think I’m missing a model in between.

RÃ©mi

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [December 4, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/how-to-associate-one-model-from-2-models/154895/2 "2008-12-04T22:40:17Z")

</div>

On Dec 4, 3:05 pm, Rémi Gagnon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is it feasible to defines associations to do that. I know that’s weird  
> a bit. I think I’m missing a model in between.
> 
> ## Rémi
> 
> Posted viahttp://www.ruby-forum.com/.

I think the design is fine, actually. If I understand correctly, your  
transactions table has columns police\_id and product\_id (and others).

If so, you can find a transaction this way:

Transaction.find\_by\_police\_and\_product(police, product)

This will return the first one that matches. You can also add further  
conditions, ordering, etc. as with any other find.

Jeff

[www.purpleworkshops.com](http://www.purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [December 5, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-associate-one-model-from-2-models/154895/3 "2008-12-05T14:26:55Z")

</div>

Thank you. But is there a way to navigate that relationship, not by a  
find?

> I think the design is fine, actually. If I understand correctly, your  
> transactions table has columns police\_id and product\_id (and others).
> 
> If so, you can find a transaction this way:
> 
> Transaction.find\_by\_police\_and\_product(police, product)
> 
> This will return the first one that matches. You can also add further  
> conditions, ordering, etc. as with any other find.
> 
> Jeff
> 
> [www.purpleworkshops.com](http://www.purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [December 5, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/how-to-associate-one-model-from-2-models/154895/4 "2008-12-05T21:59:32Z")

</div>

On Dec 5, 7:26 am, Rémi Gagnon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thank you. But is there a way to navigate that relationship, not by a  
> find?

Not sure I understand the question exactly… you can use has\_many and  
belongs\_to to help navigate:

class Police  
has\_many :transactions  
has\_many :products  
end

class Transaction  
belongs\_to :police  
belongs\_to :product  
end

class product  
has\_many :transactions  
belongs\_to :police  
end

For example:

p = Product.find(1)

You can do:

p.transactions  
p.transactions.first.police

Similarly

t = Transaction.last  
t.products  
t.products.first.police

Or maybe I misunderstood your question?

Jeff

[www.purpleworkshops.com](http://www.purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [December 6, 2008, 3:00am UTC](https://www.ruby-forum.com/t/how-to-associate-one-model-from-2-models/154895/5 "2008-12-06T03:00:46Z")

</div>

Thanks Jeff,

I was not very clear in my question.

I would like to do Police.transaction but in conjunction with Product.

But anyway today we redesign our model to make it cleaner.

Thanks for your help.

RÃ©mi

Jeff C. wrote:

> On Dec 5, 7:26ï¿½am, Rï¿½mi Gagnon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Thank you. ï¿½But is there a way to navigate that relationship, not by a  
> > find?
> 
> Not sure I understand the question exactly… you can use has\_many and  
> belongs\_to to help navigate:
> 
> class Police  
> has\_many :transactions  
> has\_many :products  
> end
> 
> class Transaction  
> belongs\_to :police  
> belongs\_to :product  
> end
> 
> class product  
> has\_many :transactions  
> belongs\_to :police  
> end
> 
> For example:
> 
> p = Product.find(1)
> 
> You can do:
> 
> p.transactions  
> p.transactions.first.police
> 
> Similarly
> 
> t = Transaction.last  
> t.products  
> t.products.first.police
> 
> Or maybe I misunderstood your question?
> 
> Jeff
> 
> [www.purpleworkshops.com](http://www.purpleworkshops.com)
