# Relations in models problem

**URL:** <https://www.ruby-forum.com/t/relations-in-models-problem/135997>\
**Category:** Rails\
**Created:** [April 27, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/relations-in-models-problem/135997 "2008-04-27T20:24:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [April 27, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/relations-in-models-problem/135997/1 "2008-04-27T20:24:44Z")

</div>

Hi!

I have a uncommon (I think problem). In my RoR app there’re 3 models:

- Transaction (date:datetime, worker\_id:integer, successful:boolean)
- Sale (amount:integer, transaction\_id:integer, medicine\_id:integer)
- Medicine (…)

Transaction is some kind of bill, is like container of Sales with one  
attached Medicine, so it looks like that:

Transaction —[0…\*]—\> Sale —[1…1]—\> Medicine

or like a tree:

Transaction  
-\> Sale -\> Medicine  
-\> Sale -\> Medicine

How to make relations? I tried to use these ones:

Transaction:  
has\_many :sales

Sale:  
belongs\_to :transactions  
has\_one :medicine

So now the problems starts, because even if I create new transaction  
and then try to make sale, nothing happens 😕 It says “Successfuly  
created new sale”, but it doesn’t show on listing.

Could anyone explain how to connect these models through relations?

---

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [April 27, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/relations-in-models-problem/135997/2 "2008-04-27T20:26:29Z")

</div>

On 27 Apr 2008, at 19:22, Pawe? K wrote:

> Transaction —[0…\*]—\> Sale —[1…1]—\> Medicine

Don’t call your model transaction. If you do then the methods that a  
generated to access your transaction will conflict with the built in  
methods for handling database transactions (and so bad stuff will  
happen).

Fred

---

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [April 27, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/relations-in-models-problem/135997/3 "2008-04-27T21:15:47Z")

</div>

Oh, thaty’s funny, but I did the same with “Transaction” changed to  
“Bill” and IT WORKS! 🙂 (at least for a moment ;D)  
Thank you very much 🙂 Probably you have a tip for naming convention  
in Rails? At this moment I know, there can’t be “Test”, “Transaction”  
etc. model name 😕 Should I use preffixes, like “myTransaction”? 😃

---

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [April 28, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/relations-in-models-problem/135997/4 "2008-04-28T21:14:58Z")

</div>

There are probably lots of names to avoid. To make sure, I’d search  
the Rails Brain API here:

[http://www.railsbrain.com/api/rails-2.0.2/doc/index.html](http://www.railsbrain.com/api/rails-2.0.2/doc/index.html)

If your proposed name (such as “transaction”) turns up a result, there  
could be a conflict…somewhere.

-Kyle
