I have a Customer and Invoice Model. A customer has many invoices and an invoice belongs_to a customer.
class Customer < ActiveRecord::Base
attr_accessible :billing_address, :customer_currency, :email, :first_name, :last_name, :mobile, :name, :payment_terms, :phase_type, :pays_vat
validates_presence_of :first_name, :last_name, :mobile, :billing_address, :payment_terms, :phase_type, :customer_currency
has_many :invoices
validates :email,
:presence => true,
:uniqueness => true,
:email_format => true
validates :name, :mobile, :presence => true, :uniqueness => true
end
Invoice Model is
class Invoice < ActiveRecord::Base
belongs_to :customer
attr_accessible :approved_by, :due_date, :invoice_date, :terms, :customer_id, :customer
validates :invoice_date, presence: true
validates :due_date, presence: true
validates :customer, presence: true
I am trying to create an index page that lists all the invoices in the system and this will invoice showing a customer Name to whom the invoice belongs. How can i retrieve that and clearly depict it in my model and view?
I think you’ve already created customer and invoice controllers and views (if not do it by
generate scaffold). Then list invoices in views\invoices\index.html.erb:Of course, you should declare
@invoicesin controllers/invoices_controller.rb