Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:35:03+00:00 2026-05-25T20:35:03+00:00

I am writing a simple accounting system for managing costs. The structure is like

  • 0

I am writing a simple accounting system for managing costs. The structure is like this:

Invoice  - can have many products
  Product - can have many costs, also can act_as_tree 
      LineItem  - 
    Product
        LineItem
        LineItem
      Product
      LineItem
  Product
      LineItem
  LineItem

I had this set up as a has_many and belongs_to for the three classes but think that the following is more appropriate (based upon reading Rails 3 Way – any shortcomings are my lack of understanding; just trying to give context)

Class Invoice < ActiveRecord::Base
    has_many :products
    has_many :line_items, :through => :products
end

 Class Product < ActiveRecord::Base
    belongs_to :invoice
    belongs_to :line_item
 end

class LineItem < ActiveRecord::Base
    has_many :products
    has_many :invoices, :through => :invoices
end

But I don’t this is working correctly.

if I do the following:

>@i=Invoice.find(1)
>@i.products # this works
>@i.line_items # doesn't work, unidentified method line_items

This is the first time I’m using has_many :through. Is this set up correctly for my data model? Also, is it possible to use it in conjunction with acts_as_tree – I’d like to be able to say:

>@i.line_items 

and get back all the line items for that specific invoice. Possible?

thx for help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:35:03+00:00Added an answer on May 25, 2026 at 8:35 pm

    First a question: What is your relation between Product and LineItem: Has 1 product many line items or is 1 and the same line item referenced in many products? The rest of this answer is based on the assumption that every product should have multiple line items.

    I think your models should be defined like that:

    # No change
    Class Invoice < ActiveRecord::Base
      has_many :products
      has_many :line_items, :through => :products
    end
    
    # Changed the relation to :line_items
    Class Product < ActiveRecord::Base
      belongs_to :invoice
      has_many :line_items
    end
    
    class LineItem < ActiveRecord::Base
      belongs_to :products
      # The following then does not make sense
      ##has_many :invoices, :through => :invoices
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple web app in PHP that needs to have write access
I'm writing a simple game and I'm going to have the mouse control the
While writing a simple server-client application, this question came in my mind. When someone
I'm writing a (simple) compiler in Scala and have made the tokenizer iterable and
I am writing simple blog with CodeIgniter and DataMapper and I have problem with
This is a really basic ruby gems question. I'm familiar with writing simple ruby
I'm writing a simple accounting program consists of several C# winform clients and a
I'm writing a simple web application and have hit a stumbling block of sorts
I'm writing a simple app that's going to have a tiny form sitting in
I am writing a simple database with web access. I have previous experience with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.