json - Rails - preprocessing in DB layer (PG) -


given use postgres , have method:

  def undelivered_messages(datetime)     messages = {}     @current_user.conversations.includes(:user_messages).each |c|       messages[c.id] = c.user_messages.where('user_id != ? , created_at > ?', @current_user.id, datetime)     end     json.generate(messages)   end 

i expect output (not counting json.generate()):

{1=>[m1,m2,m3], 2=>[m1,m2,m3]} 

i wonder, can thing sql query @ db level, db prepares hash (or json object), faster, ruby slow, , databases not.

you can try this:

usermessage  .joins(:conversation)  .where('user_messages.user_id != ? , user_messages.created_at > ?', @current_user.id, datetime)  .where(conversations: { user_id: @current_user.id })  .group_by(&:conversation_id) 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -