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

  • Home
  • SEARCH
  • 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 8770147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:30:22+00:00 2026-06-13T17:30:22+00:00

First, some background. My application models are like so: User has_many :subscriptions; has_many :courses,

  • 0

First, some background.

My application models are like so:

  User has_many :subscriptions; has_many :courses, :through => :subscriptions
Subscription belongs_to :course, :user
  Course has_many :subscriptions; has_many :users, :through => :subscriptions

Subscription is a join table used to store information such as state (:active, :inactive, :failed, :passed, :unpaid) as well as serve as a connector to all the information that comes with taking a course (such as the Grade model(not yet added), the Payment model(work in progress), etc).

I want to add the Payment model to store a history of payments on a subscription. Each payment can be for one or more subscriptions. So far, I’ve tried a Payment has_many :subscriptions, but I’m having trouble with the controller logic and I’m thinking maybe there’s a better way to associate the models for “a Payment from a User on a Subscription to a Course.”

Technically speaking, in a payment show action, I would need information from each of these models (course.name, user_id of user who paid, payment.amount, all of which are connected to a Subscription) and in a Payment.create action, I’d need to set the state of the subscription (that I can do).

If this is a logical/efficient way to associate the various models, how do I access the needed information(some information from each model course, user, payment, and subscription) in the Payment show action with minimal database calls? I’m using Rails 3.2/Ruby 1.9 and would like to stick with ActiveRecord/ARel instead of straight sql statements. I’m open to “you’re doing it wrong” answers as well. If changing the associations will make it more efficient to access the tables from the controller, I’m open to that as well.

  • 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-13T17:30:23+00:00Added an answer on June 13, 2026 at 5:30 pm

    @D3mon-1stVFW’s answer set me on the right track to figure this problem out.

    For the Payment show action, the queries would go something like:

    @payment = Payment.find_by_id(params[:id])
    @subscriptions = @payment.subscriptions.includes(:course, :user)
    

    This will make it possible to access the course and user data associated with a subscription like so:

    <% @subscriptions.each do |subscription|%>
      <%= subscription.payment.amount %>
      <%= subscription.user.email %>
      <%= subscription.course.name %>
      <%= subscription.state  %>
    <% end%>
    

    For a payment new/create action where there is not yet a payment.id, the queries are slightly different:

    @unpaid_subs = Subscription.where(:user_id => current_user.id).unpaid.includes(:course)
    @payment = Payment.new
    

    Where current_user is the user that is going to pay(create a new payment) and .unpaid is a scope on the Subscription model that looks like this:

    scope :unpaid, -> { where('subscriptions.payment_id' => nil)}
    

    And in the view, you can access the data in the same way as with the show view:

    <% @unpaid_subs.each do |sub| %>
      <%= sub.course.name %> | <%= sub.course.credits %><br />
    <% end %>
    

    My main point of confusion had been that I didn’t know you could access parent associations from the child. Subscription belongs_to Courses, yet you can still do subscription.course.something.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First some background: I'm working on an application and I'm trying to follow MVVM
First, some background: I'm developing a web application using Python. All of my (text)
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
First Some Background I'm planning out the architecture for a new PHP web application
First some background information: We have three environments for our EJB3 application: test, development
Some background: I am writing a application with several forms, etc. Users have to
First some background, I am currently working on a relatively large Asp.Net MVC application
First some background on the application. I have an application processing many independent tasks
First some background. We have a vendor application which generates logs and configuration files

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.