Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Integrating 2 ActiveRecord Obj...

Replytotopic

Integrating 2 ActiveRecord Objects

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

I have two ActiveRecord Models where I want to use the find (:all, :order=>‘created_at DESC’) method (or sql) so it output a collection of both objects together listed in descending order by date. Does anyone have any ideas how to do this?

 
Laleephoto3_150

Authority 12
Posting Rating 93
Sign in to rate this post

You could try something like this, wrapped into a method called first_and_next_collection defined on both of your models:

FirstModel.find(:all).concat(NextModel.find(:all)).sort{|a,b| a.created_at <=> b.created_at}

 
Profile

Authority 12
Posting Rating 95
Sign in to rate this post

Try using :include

like

FirstModel.find(:all, :include => :next_model, :order=>‘created_at DESC’)

Replytotopic

Other Recent Topics

Ask a Rails expert : Sanitizing html

Ask a Rails expert : First post, requesting sage perspective

Ask a Rails expert : How to use mephisto

Ask a Rails expert : How to use mephisto

Ask a Rails expert : will_paginate customization problem

Ask a Rails expert : BackgroundRB still wants 'development' environment...?

Ask a Rails expert : activescaffold, sql exception

Ask a Rails expert : Passing non-english chars in query string

Ask a Rails expert : Rails and 2D barcodes

Ask a Rails expert : apache giving proxy error

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel