# Model Relation - has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/model-relation-has-and-belongs-to-many/157186>\
**Category:** Rails\
**Created:** [January 8, 2009, 9:00am UTC](https://www.ruby-forum.com/t/model-relation-has-and-belongs-to-many/157186 "2009-01-08T09:00:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![indu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/eada6e/32.png) [@indu](https://www.ruby-forum.com/u/indu)\
**Post date:** [January 8, 2009, 9:00am UTC](https://www.ruby-forum.com/t/model-relation-has-and-belongs-to-many/157186/1 "2009-01-08T09:00:14Z")

</div>

Hello everyone  
I am very much confused about specifying the relation in the following  
one. I had Item, Invoice, Invoicedetail model.  
Item table contains - id and name,unit\_price fields.  
Invoice table contains - id, invoice\_number and date  
Invoicedetails - invoice\_id, item\_id, quantity, unit\_price fields.  
I had mentioned the following relations.  
class Item \< ActiveRecord::Base  
has\_many :invoicedetails  
has\_many :invoices, :through=\>:invoicedetails  
end  
class Invoicedetail \< ActiveRecord::Base  
belongs\_to :item  
belongs\_to :invoice  
end  
class Invoice \< ActiveRecord::Base  
has\_many :invoicedetails  
has\_many :items, :through=\>:invoicedetails  
May anyone suggest I am specifying the correct relation or not? If it is  
wrong please give correct one and suggestions for understanding the  
specification of relations more.  
Thanks in advance
