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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:56:25+00:00 2026-05-30T09:56:25+00:00

I am trying to display and capture only the most recent item from a

  • 0

I am trying to display and capture only the “most recent” item from a *has_many* relationship. Basically, I want to find a single object that exists in a *has_many* relationship (based on some criteria) and display that with the record.

I have the class relationships defined like so (stripped down):

class Member < ActiveRecord::Base
    has_many :member_status_histories
    has_many :member_statuses, :through => :member_status_history
end

class MemberStatusHistory < ActiveRecord::Base
    belongs_to :member_status
    belongs_to :member
    has_one :timespan
end

class MemberStatus < ActiveRecord::Base
    attr_accessible :name
    has_many :memberStatusHistories
    has_many :members, :through => :memberStatusHistories
end

class Timespan < ActiveRecord::Base
    attr_accessible :startDate, :endDate
    belongs_to :timespanable, :polymorphic => true
end

Specifically using the classes and relationships above, I want to have a page that displays a member object and the one member status history object that does not have an end date. If the user changes the fields on the page for the member status history, I want to take that information and make a new member status history object and save that to the member_status_histories in member.

I have tried several things, but granted that I’m new to RoR, I’m not sure what approach to use to do this. It seems like there are several ways to try to get this to possibly work, but I haven’t been able to get any of them to fully work.

The idea that seemed to make the most sense to me was using a virtual attribute. The virtual attribute would provide the member status history object that I wanted to display, and then when the user submitted the updated member and member status history, I could run some business logic on the virtual attribute (using before_save) and update the *member_status_histories* field as needed.

Again, I have been unable to get any approach to work so I am open to suggestions on how to accomplish this. Thanks!

  • 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-30T09:56:26+00:00Added an answer on May 30, 2026 at 9:56 am

    I’d ditch the concept of multiple MemberStatusHistories. It’ll quickly get very complicated and luckily for you there’s already a gem that does everything you want called paper_trail. In member_status_history.rb:

    class MemberStatusHistory < ActiveRecord::Base
        has_paper_trail
        belongs_to :member_status
        belongs_to :member
        has_one :timespan
    end
    

    Then you can do @member.member_status_history.previous_version to see the version before the current member_status_history, and the current one is always the “live” one at @member.member_status_history. That should allow you to always see the “most recent” member status history while constantly having the ability to an arbitrary number of remaining ones from your paper_trail version table.

    For more information on how that all works, check out the paper_trail github documentation.

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

Sidebar

Related Questions

I'm trying to display video from a webcam. I capture the images from the
When trying to display a byte stream from HLDS (Half-Life Dedicated Server) in a
I'm trying to display a series of titles varying from 60 characters to 160
I'm trying to capture or record the vector display information of a WPF (maybe
I'm trying to capture output from a console application by running it in a
I am trying to capture video from a camera. i have gotten the captureOutput:didOutputSampleBuffer:
From my script/ directory in a rails app, I am trying to capture the
I am trying to capture screenshots using the BitBlt function. However, every single time
I am trying to capture a still frame from an (any) external swf file,
Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s

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.