I have two separate models MarketOrder and OnlineOrder.
Two models do not have any relationship between them but need to be displayed in one table.
Sorting and Pagination required. Simple solution would be:
orders = (MarketOrder.all + OnlineOrder.all).sort_by(&:created_at).paginate(per_page: 50, page: params[:page])
This works fine but it is not suitable as number of orders constantly grows. Query all records would cause a huge drop in performance. How correctly to narrow such query? Thanks
Update
Models do not have exactly the same columns but share some common ones which need to be displayed in my view file.
MarketOrder(
id: integer,
user_id: integer,
service_fee_amount: decimal,
status: string, price: decimal,
created_at: datetime,
updated_at: datetime..
)
OnlineOrder(
id: integer,
user_id: integer,
category: string,
comment: text,
status: string,
price: decimal,
created_at: datetime,
updated_at: datetime...
)
Please note that both models are independent and Ids in both go 1,2,3... 1,2,3...
But each model has a relation with a third model User (belongs_to <=> has_many) and where each table contains user_id.
as per sort_by(&:created_at).paginate(per_page: 50, page: params[:page]) 50 most recent orders would be prepared with jbuilder and send to react component. To get next 50 orders the request would be made again to backend.