Forum: Ruby on Rails Model Relation - has_and_belongs_to_many

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
57c6a3276ed9cbb3b271e5dd067f0724?d=identicon&s=25 Indu R Nair (indu)
on 2009-01-08 09:00
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
This topic is locked and can not be replied to.