1

I want to render all objects of a certain class, including the latest associated element when, the index-action of a controller is called.

Here is the code for my chatroom-model. A chatroom can have many subscribers:

class Chatroom < ApplicationRecord
   has_many :subscribers, dependent: :destroy
end

When a user visits the starting page, he should see all chatrooms and the most recently created subscriber for each chatroom (not the most recently associated).

My controller looks like this. It does not work and I'm cofident, that there is a method to somehow limit the (:include => :subscribers) command or so:

class ChatroomsController < ApplicationController
   # GET /chatrooms
   def index
      @chatrooms= Chatroom.all.includes(Subscriber.where("subscriber.created_at < ?", Time.now).order('created_at DESC').first)
      render json: @chatrooms
   end
end

I struggle to find a solution on how to select the correct subscriber object. Could you please help me with this issue?

mfaorlkzus
  • 149
  • 3
  • 14
  • 1
    What happens if you have less chatrooms than subscribers, or viceversa? I guess as you want related and non-related subscribers you should manage them in two separate queries. – Sebastián Palma Feb 24 '18 at 18:03
  • No, all subscribers are related to a chatroom object, but i want to render just the most recent one. If there are non for a chatroom, it's ok, but when there are more than one, I have to filter out all but the most recent subscriber – mfaorlkzus Feb 24 '18 at 18:51

1 Answers1

1

If you want to eagerly load the latest associated subscriber for each chatroom, you can add has_one association with scope to your Chatroom model:

class Chatroom < ApplicationRecord
   has_many :subscribers, dependent: :destroy
   has_one :latest_subscriber, -> { order(created_at: :desc) }, class_name: 'Subscriber' 
end

And include it:

@chatrooms = Chatroom.includes(:latest_subscriber).all

When a user visits the starting page, he should see all chatrooms and the most recently created subscriber (not the most recently associated).

For loading one, the most recently created subscriber, without association to particular chatroom, you should use the separate query. For example:

@most_recent_subsciber = Subscriber.order(created_at: :desc).first

Then just build JSON-response that will consist of chatrooms array to render them separately with ability to also render the latest subscriber (if you include it in render json) for each chatroom, and the most recently created subscriber.

Maksim Kalmykov
  • 1,293
  • 3
  • 20
  • 26
  • 1
    Thank you for the suggestion, the latest_subscriber association shold work for me. And sorry for the confusion, I meant the most recently created subscriber for each chatroom. So I want to query for each chatroom out of all associated subscribers the one that was created last. – mfaorlkzus Feb 24 '18 at 18:54
  • It does, but can you explain me your syntax? It doesn't work for me and I have never seen such a structure in Rails^^ – mfaorlkzus Feb 24 '18 at 19:10
  • @mfaorlkzus Join [the chat room](https://chat.stackoverflow.com/rooms/165760/rails-api-limit-includes-results-to-only-the-latest-created-record). – Maksim Kalmykov Feb 24 '18 at 19:13