How to join only one record from a related table with Rails?

Viewed 56

In my rails application, I have two tables

Member
id
name
phone
Membership
id
duration
start_date
member_id

I'm trying to generate a query to join members with memberships but I only want the current membership which is the one with the most recent start_date for each member to be joined.

I came up with this

Member.joins(:memberships).merge(Membership.order(start_date: :desc).distinct(:member_id))

But it doesn't work and errors out with the message "(Object doesn't support #inspect)". Struggling with this, would really appreciate some help! Thank you :)

1 Answers
Related