Suavocado
Suavocado

Reputation: 969

Rails/Activerecord use sort_by with multiple models and different attribute names for the sorting dates

I have a Rails app and I want to include a list of orders, quotes, etc. I currently include these in the view using.

  def dashboard
    rfqs = Rfq.all.where(is_active: true)
    rfis = Rfi.all
    orders = Order.all
    order_reminders = OrderReminders.all
    @posts = (rfqs + rfis + orders).sort_by(&:created_at)
  end

in the last line, I would like to use different attributes to sort the @posts with.
So every @post has a date to order by but with different names.

rfqs have a "due" attribute

rfis have a "date" attribute

orders have a "ship" attribute.

Upvotes: 0

Views: 215

Answers (1)

penner
penner

Reputation: 2737

@posts = (rfqs.to_a + rfis.to_a + orders.to_a).sort_by do |post|
  if post.is_a?(Rfq)
    post.due
  elsif
    ..
  else
    ..
  end
end

Upvotes: 1

Related Questions