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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:18:54+00:00 2026-05-24T03:18:54+00:00

I have a fairly standard ‘has_many :x, :through => :y’ relationship with a user,

  • 0

I have a fairly standard ‘has_many :x, :through => :y’ relationship with a user, a problem, and a completed_problem which acts as the association between the two:

/models/user.rb

class User < ActiveRecord::Base
  has_many :completed_problems
  has_many :problems, :through => :completed_problems    
end

/models/problem.rb

class Problem < ActiveRecord::Base
  belongs_to :wall
  has_many :completed_problems
  has_many :users, :through => :completed_problems    
end

/models/completed_problem.rb

class CompletedProblem < ActiveRecord::Base
  belongs_to :user
  belongs_to :problem

  validates_presence_of :user
  validates_presence_of :problem
end

My complication is that data in each of these models impacts the display. I’m looking to display a list of problems on each wall, and for each problem in that list to use or show:

  • problem.id
  • problem.name
  • time since the current user completed the problem last
    • if no logged in user, some text
    • if user hasn’t completed that problem, some other text

A (very ugly) first pass at the view is as follows:

/views/walls/show.html.erb

<% @wall.problems.each do |problem| %>
    <a id=<%= "problem_#{problem.id}" %>>
      <h3><%= problem.name %></h3>
      <p><%= "#{time_ago_in_words(problem.last_complete_by_user(current_user))} ago" if current_user && problem.last_complete_by_user(current_user) %></p>
    </a>
  </li>
<% end %>

I’ve since overwritten it, but problem.last_complete_by_user (seen in the above snippet) was an attempt to use the problem object to find all the related completed_problems, with the user as an argument, in order to identify the ‘updated_at’ value for the most recently updated completed_problem for that particular problem and user.

Of course this isn’t ideal because it’ll be a separate query for each item in the list – I assume the preferred solution would be a method in the wall controller or model that joins across all 3 tables and returns a new array for the view to iterate over. Unfortunately I’ve spent too long bouncing between :join, :include and :find_by_sql without a solution.

Can someone at least lead me in the right direction for how to get this view working properly?

  • 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-24T03:18:55+00:00Added an answer on May 24, 2026 at 3:18 am

    This is how I would solve the problem. It may not be the most efficient solution, but it’s clean and easy to refactor when the time comes. I haven’t tried the code, but it’s probably not too far off. If you go this route and run into performance problems, I would look into fragment caching before adding a bunch of crazy SQL.

    Models:

    class User < ActiveRecord::Base
      has_many :completed_problems
      has_many :problems, :through => :completed_problems
    
      # Finds the last completed problem
      def last_completed_problem(problem)
        problems.order('created_at DESC').where(:problems => {:id => problem}).limit(1).first
      end
    end
    
    # No Changes
    class Problem < ActiveRecord::Base
      belongs_to :wall
      has_many :completed_problems
      has_many :users, :through => :completed_problems
    end
    
    # No changes
    class CompletedProblem < ActiveRecord::Base
      belongs_to :user
      belongs_to :problem
    
      validates_presence_of :user
      validates_presence_of :problem
    end
    

    app/controllers/walls_controller.rb:

    class WallsController < Application::Controller
      def show
        @wall = Wall.find(params[:id]).includes(:problems)
      end
    end
    

    app/helpers/wall_helper.rb:

    module WallHelper
      def show_last_completed_problem_for_user(user, problem)
        return "You are not logged in" if current_user.nil?
    
        completed = user.last_completed_problem(problem)
    
        return "You have not completed this problem" if completed.nil?
    
        time_ago_in_words(completed.created_at)
      end
    end
    

    app/views/walls/show.html.erb:

    <%= render :partial => 'problem', :collection => @wall.problems %>
    

    app/views/walls/_problem.html.erb:

    <li>
      <a id=<%= "problem_#{problem.id}" %>>
        <h3><%= problem.name %></h3>
        <p><%= show_last_completed_problem_for_user(current_user, problem) %></p>
      </a>
    </li>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Rails app I have a fairly standard has_many relationship between two entities.
I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have
I have a fairly standard rails app and would like to be able to
I have a fairly simple addition to the HTTP standard. An ambitious goal I
We have fairly large C++ application which is composed of about 60 projects in
I have a fairly standard ASP.NET GridView that displays 2 columns that have a
I have a fairly standard OLTP normalised database and I have realised that I
I have a fairly standard .Net MVC Controller method: public ActionResult Add(Customer cust) {
I have a fairly standard blog app with the usual post and comments controller/models
I have a fairly standard situation: Click a button, it loads a transition page

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.