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 9206201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:07:32+00:00 2026-06-18T00:07:32+00:00

In my Rails (3.2) app, an Order has many LineItems . A LineItem has

  • 0

In my Rails (3.2) app, an Order has many LineItems. A LineItem has many LineItemPayments. A LineItemPayment has one Payment. (LineItems can potentially be payed for multiple times (subscriptions), which is why I have the join table there.)

I need to be able to query for order information from a payment record. I can get an array of orders via relationships, but I know they will always be the same order. Is there a way in Rails to set up the association to reflect this? If not, would it better to set up a method for retrieving the array and then picking the order out of that, or rather just storing the order_id with the payment and set up a direct relationship that sidesteps all this?

  • 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-06-18T00:07:33+00:00Added an answer on June 18, 2026 at 12:07 am

    You’ll need to work with the orders collection and narrow it down accordingly per your own logic. Although you certainly ‘can’ add the order_id to the payment directly, that will denormalize your data (as a cache) which is only recommended when you start hitting performance bottlenecks in your queries – otherwise it’s asking for trouble in the area of data integrity:

    class Payment < ActiveRecord::Base
      has_many :line_item_payments
      has_many :line_items, :through => :line_item_payments
      has_many :orders, :through => :line_items
    
      # use this to get the order quickly
      def order
        orders.first
      end
    
      # use this to narrow the scope on the query interface for additional modifications
      def single_order
        orders.limit(1)
      end
    end
    
    class LineItemPayment < ActiveRecord::Base
      belongs_to :line_item
      belongs_to :payment
    end
    
    class LineItem < ActiveRecord::Base
      belongs_to :order
      has_many :line_item_payments
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following relation in my rails app: genre - has many -
I have the following relation in my rails app: genre - has many -
This one has got me stumped. I'm deploying a Rails 3 app to Slicehost
I have a model User and Listing in my rails app. User has many
Say that a Rails app has a Car and an Order model. The Car
My Rails app has some pages which are SSL-required and others which are SSL-optional.
Rails newbie here, writing a sample app that has the following three models and
I have a Ruby on Rails app that has a scheduled job I execute
I have a Rails app with an Order and a Refund model. Order has_many
I have an Address model and an Order model in my Rails app. An

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.