Relations in models problem


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:

-> Sale -> Medicine
-> Sale -> Medicine

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

has_many :sales

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 :confused: It says “Successfuly
created new sale”, but it doesn’t show on listing.

Could anyone explain how to connect these models through relations?

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


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

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

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


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs